【问题标题】:How to put data in html with good format [duplicate]如何以良好的格式将数据放入html中[重复]
【发布时间】:2019-04-27 00:02:31
【问题描述】:

我正在使用 Django,并且我有我在 html 中使用的模型信息

我遇到的问题

<img src="{% static 'Bola/noticias_imagens/{{ noticia.noticia_imagem }}' %}"

如何使这段代码工作?

我还有一个文本,其中包含一些标签,例如 &lt;p&gt;&lt;br&gt;,当我执行此代码时,它还会在网站中逐字显示标签 {{ noticia.noticia_texto }} 显示的文本示例:

"Hello.&lt;p&gt;Goodbye&lt;/p&gt;"

有办法解决吗?

【问题讨论】:

    标签: html


    【解决方案1】:

    第一个问题:

    {% with 'Bola/noticias_imagens/'|add:noticia.noticia_imagem as image_static %}
      <img src="{% static image_static %}">
    {% endwith %}
    

    第二个问题:

    • 您可以使用过滤器{{ noticia.noticia_texto | safe }}
    • 你可以使用标签{% autoescape off %}{{ noticia.noticia_texto }}{% endautoescape %}

    但您必须了解自己在做什么,因为这不安全,并且可能导致 HTML 注入攻击。

    【讨论】:

      猜你喜欢
      • 2011-07-13
      • 2010-12-20
      • 2010-11-08
      • 1970-01-01
      • 2011-02-03
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多