Django,自动 HTML “清理”当将HTML放入模板时,如何停止它?

发布于 2024-09-16 00:45:33 字数 1398 浏览 4 评论 0原文

我对此感到有点困惑,因为 Django 模板似乎有可选的 HTML 过滤器,但这似乎是自动发生的。我正在制作这个演示应用程序,用户将执行一个调用 python 脚本来检索 url 的操作,然后我想在新窗口中显示它..一切都很好,除了当显示返回时,HTML 以这种格式进行清理(当我查看页面源代码时,我看到了这一点,在浏览器中它显示为常规 HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="content-type" /><script type="text/javascript">//<![CDATA[
si_ST=new Date

:是相同的常规 HTML 版本:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="content-type" /><script type="text/javascript">//<![CDATA[ si_ST=new Date //]]></script><script type="text/javascript">//<![CDATA[ _G={ST:(si_ST?si_ST:new Date),Mkt:"en-

我只是将其输出到 html 模板中的基本块,该模板没有其他格式(没有 HTML 等),只有 1 个块,此输出所在的位置.. 关于原因的任何建议这种情况正在发生,如何显示常规 HTML(因此它将在浏览器中显示页面而不是 HTML 文本)。谢谢。

I'm kind of confused by this because it seems that Django templates have optional HTML filters but this seems to be happening automatically.. I am making this demo app where the user will do an action that calls a python script which retrieves a url, I then want to display this in a new window.. its all fine except when the display comes back, the HTML is sanitized in this format (I see this when I view the page source, in the browser it shows as regular HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="content-type" /><script type="text/javascript">//<![CDATA[
si_ST=new Date

this is the regular HTML version of the same:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="content-type" /><script type="text/javascript">//<![CDATA[ si_ST=new Date //]]></script><script type="text/javascript">//<![CDATA[ _G={ST:(si_ST?si_ST:new Date),Mkt:"en-

I'm just outputting this to a basic block in my html template, the template has no other formatting (no HTML, etc), just 1 block where this output goes.. any advice on why this is happening and how to display the regular HTML (so it would show the page in the browser and not the HTML text) is appreciated.. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橙味迷妹 2024-09-23 00:45:34

使用安全过滤器:

{{ myvariable|safe }}

如果您需要像这样处理模板的大部分内容(也就是说,如果您发现自己一遍又一遍地使用 |safe ),您可以禁用自动转义批发:

{% autoescape off %}
blah {{myvariable}} blah {{myothervariable}}
{% endautoescape %}

Use the safe filter:

{{ myvariable|safe }}

If you need large parts of your template treated like this (that is, if you find yourself using |safe over and over), you can disable the autoescaping whole-sale:

{% autoescape off %}
blah {{myvariable}} blah {{myothervariable}}
{% endautoescape %}
丘比特射中我 2024-09-23 00:45:34

看一下“safe”过滤器,它禁用 Django 的默认转义:

http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#safe

Take a look at the "safe" filter, which disables Django's default escaping:

http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#safe

蘑菇王子 2024-09-23 00:45:34

还有 autoescape 控制转义块宽。

There is also autoescape which controls escaping block wide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文