简体   繁体   English

从Django中的视图返回HTML元素

[英]Returning HTML elements from a view in Django

I'm trying to return an image from a view to a template in Django. 我试图将图像从视图返回到Django中的模板。 Currently my view has an array I want to display in a table, but in different ways (most elements as strings but one in particular as an image) 目前,我的视图中有一个数组,我想在表格中显示,但方式不同(大多数元素为字符串,但特别是图像)

items = [
  ['an apple', '<img src="apple.png" \\>'], 
  ['a pear', '<img src="pear.png" \\>'], 
  ['an orange', <img src="orange.png" \\>]
]

and in my template, 在我的模板中

<table>
{% for fruit in items %}
  <tr>
  {% for element in fruit %}
    <td>
      {{ element }}
    </td>
  {% endfor %}
  </tr>
{% endfor %}
</table>

This prints out the literal string instead of displaying the image pear.png. 这将打印出文字字符串,而不是显示图像pear.png。

Is there a way I can tell the template to insert the element into the code rather than render as a string? 有没有办法告诉模板将元素插入代码中,而不是呈现为字符串?

The filter safe marks a string as not requiring further HTML escaping prior to output. 过滤器safe将字符串标记为不需要在输出之前进一步转义HTML。

<table>
{% for fruit in items %}
  <tr>
  {% for element in fruit %}
    <td>
      {{ element | safe }}
    </td>
  {% endfor %}
  </tr>
{% endfor %}
</table>

Sidenote: You can also convert your items structure to a dict for easier and clearer usage: 旁注:您还可以将项目结构转换为字典,以便更轻松,更清楚地使用:

items = {
  'an apple': '<img src="apple.png" />', 
  'a pear': '<img src="pear.png" />', 
  'an orange': '<img src="orange.png" />'
}

Template: 模板:

<table>
{% for fruit, img in items.items %}
  <tr>
    <td> {{ fruit }}
    <td> {{ img | safe }} </td>
  </tr>
{% endfor %}
</table>

Lastly, you can also only add the image source in your dict and avoid having to use safe : 最后,您也只能在字典中添加图片源,避免使用safe

items = {
  'an apple': 'apple.png', 
  'a pear': 'pear.png', 
  'an orange': 'orange.png'
}

Template: 模板:

<table>
{% for fruit, img in items.items %}
  <tr>
    <td> {{ fruit }}
    <td> <img src="{{ img }}" /> </td>
  </tr>
{% endfor %}
</table>
 {% for fruit, image_tag in items %}
     <td> {{fruit}} </td>
     <td> {{image_tag|safe}} </td>
    {% endfor %}

you need to mark the string as safe for the xss protection https://docs.djangoproject.com/en/1.8/ref/templates/language/#automatic-html-escaping 您需要将字符串标记为安全的xss保护https://docs.djangoproject.com/en/1.8/ref/templates/language/#automatic-html-escaping

If you don't want data to be auto-escaped, on a per-site, per-template level or per-variable level, you can turn it off in several ways. 如果您不希望在每个站点,每个模板级别或每个变量级别自动转义数据,则可以通过多种方式将其关闭。

Why would you want to turn it off? 您为什么要关闭它? Because sometimes, template variables contain data that you intend to be rendered as raw HTML, in which case you don't want their contents to be escaped. 因为有时模板变量包含您打算呈现为原始HTML的数据,在这种情况下,您不希望转义其内容。 For example, you might store a blob of HTML in your database and want to embed that directly into your template. 例如,您可能在数据库中存储了HTML一滴,并希望将其直接嵌入到模板中。 Or, you might be using Django's template system to produce text that is not HTML – like an email message, for instance. 或者,您可能正在使用Django的模板系统来生成非HTML的文本,例如电子邮件。 For individual variables¶ 对于单个变量

To disable auto-escaping for an individual variable, use the safe filter: 要禁用单个变量的自动转义,请使用安全过滤器:

This will be escaped: {{ data }} This will not be escaped: {{ data|safe }} 这将被转义:{{data}}这将不会被转义:{{data | safe}}

You can use 您可以使用

{% for fruit, fruitimage in items %}
 <td> {{fruit}} </td>
 <td> <img src="{{fruitimage}}"> </td>
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM