【问题标题】:django render html tags passed from views.pydjango渲染从views.py传递的html标签
【发布时间】:2017-07-11 22:30:41
【问题描述】:

我希望我的用户能够编写,例如 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 中的内容是什么?它的imgtext

标签: python django


【解决方案1】:
{{ articleTXT |safe }}

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

应该做的伎俩

*注意:它的 {% endautoescape %}

如果这不起作用尝试,

format_html(articleTXT)

在你看来。

希望这会有所帮助:)

【讨论】:

  • format_html 为我工作!!其余的没有!所以解决方案是添加 from django.utils.html import format_html 然后使用 processesText = format_html(processed_Text) 谢谢谢谢谢谢
  • 真棒 :D 很高兴它有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
  • 2016-10-01
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
相关资源
最近更新 更多