Django中的文字HTML不使用变量? [英] Literal HTML in Django without using a variable?

查看:160
本文介绍了Django中的文字HTML不使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆HTML:

<div align="center" id="whatever">
<a href="http://www.whatever.com" style="text-decoration:none;color:black">
<img src="http://www.whatever.com/images/whatever.jpg" />
</a></div>

我想在浏览器中将其全部输出为文字HTML,即向用户显示上述内容。 (编辑:不幸的是,我没有访问我的模板中的Django变量,由于我正在处理的系统的怪癖,所以我不能使用 {{| escape}}

I want to output it all as literal HTML in the browser, i.e. display the above to the user. ( unfortunately I don't have access to Django variables in my template, due to the quirks of the system I'm working on, so I can't use {{ | escape }}.

有没有办法这样做,要么使用Django模板标签或特殊的HTML标签,无需HTML全部手动?

Is there any way to do this, either using Django template tags or a special HTML tag, without having to HTML-escape it all by hand?

谢谢!

推荐答案

Yup,只需使用 escape filter:

Yup, just use the escape filter:

{{my_html|escape}}

修改 force_escape p>

Edit: force_escape:

{% filter force_escape %}
   ... your HTML to be escaped ...
{% endfilter %}

或者,从Python:

Or, from Python:

from django.utils.html import escape
escaped_html = escape(html_to_escape)

这篇关于Django中的文字HTML不使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆