Django渲染HTML标签从Views.py传递



我希望我的用户能够写作,例如 image1 ,并使用正则表达式来识别并用标签替换。我所有这些都可以工作 - 但是当我将数据传递给模板以进行显示时,我会这样这样做:

    <div class="container">
        <p>
            {% articleTxt %}
        </p>
    </div>

articletxt包含:

This is a test Article <img src="/media/articleImages/django-allauth.png"> 
and that was a picture and this is another one then some more text ---- 
original: This is a test Article *image1* and that was a picture and this is 
another one *image2* then some more text

那是我的沮丧。我尝试将模板更改为:

{% autoescape off %} {{articleTXT}} {{% autoescape %}}

我也尝试了

{{% articleTXT|safe %}}

它总是打印标签而不是渲染标签。无论如何,是否可以渲染从Views.py到我的模板传递的文本中嵌入的标签!

{{ articleTXT |safe }}

{% autoescape off %} {{ articleTXT }} {% endautoescape %}

应该做技巧

*注意:它{%endautoescape%}

如果这不起作用,

format_html(articleTXT)

在您的视图中。

希望这会有所帮助:)

最新更新