首页 > 解决方案 > 如何以良好的格式将数据放入html中

问题描述

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

我遇到的问题

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

如何使此代码工作?

同样,我有一个带有一些标签的文本,<p><br>我执行此代码时,它还会在网站中按字面意思显示标签 显示 {{ noticia.noticia_texto }} 的文本示例:

"Hello.<p>Goodbye</p>"

有没有办法解决它?

标签: html

解决方案


第一个问题:

{% 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 注入攻击。


推荐阅读