Django,自动 HTML “清理”当将HTML放入模板时,如何停止它?
我对此感到有点困惑,因为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用安全过滤器:
如果您需要像这样处理模板的大部分内容(也就是说,如果您发现自己一遍又一遍地使用
|safe
),您可以禁用自动转义批发:Use the safe filter:
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:看一下“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
还有 autoescape 控制转义块宽。
There is also autoescape which controls escaping block wide.