简体   繁体   English

Django模板转义

[英]Django template escaping

Django templating system provides a few options (filters) for escaping contents in the html, but they are kind of confusing to me as a beginner. Django模板系统提供了一些用于在html中转义内容的选项(过滤器),但是作为初学者,它们让我感到困惑。 Say I'm following a tutorial to make a simple blog, and the blog content needs to be escaped - I trust the content because I am the only one editing it. 假设我正在按照教程制作一个简单的博客,并且博客内容需要转义 - 我相信内容,因为我是唯一一个编辑它的人。 So the question is should I do it like {{ post.content|autoescape }} , {{ post.content|escape }} , or {{ post.content|safe }} in the html? 所以问题是我应该在html中执行{{ post.content|autoescape }}{{ post.content|escape }}{{ post.content|safe }}吗?

Thanks 谢谢

EDIT: Which filter should I use to have special characters converted to html entities automatically? 编辑:我应该使用哪个过滤器将特殊字符自动转换为html实体?

EDIT 2: I just realized that autoescape is not a valid filter. 编辑2:我刚刚意识到autoescape不是一个有效的过滤器。

HTML escaping is on by default in Django templates. 默认情况下,Django模板中的HTML转义处于启用状态。

Autoescape is a tag. Autoescape是一个标签。 not a filter: 不是过滤器:

{% autoescape on %}
    {{ post.content }}
{% endautoescape %}

The 'escape' filter escapes a string's HTML. 'escape'过滤器转义字符串的HTML。 Specifically, it makes these replacements: 具体来说,它使这些替换:

  • < is converted to &lt; <被转换为&lt;
  • > is converted to &gt; >转换为&gt;
  • ' (single quote) is converted to &#39; '(单引号)转换为&#39;
  • " (double quote) is converted to &quot; “(双引号)转换为&quot;
  • & is converted to &amp; &转换为&amp;

The 'force_escape' is almost identical to 'escape' except for a few corner cases. 'force_escape'与'escape'几乎完全相同,除了一些极端情况。

The 'safe' filter will mark your content as safe, so it won't be escaped (will be sent to browser as is). “安全”过滤器会将您的内容标记为安全,因此不会对其进行转义(将按原样发送到浏览器)。

Which filter should I use to have special characters converted to html entities automatically? 我应该使用哪个过滤器将特殊字符自动转换为html实体?

Well, you mean, like converting à to &Atilde; 嗯,你的意思是,就像转换Ã&Atilde; ? Stick with utf-8 encoding all the way and forget about those. 坚持使用utf-8编码并忘记这些。

first of all, you should escape your content because you never know (even if you are the one who enter the data) if you are going to need special character (like <, >, ). 首先,你应该逃避你的内容,因为如果你需要特殊的角色(比如<,>),你就不会知道(即使你是输入数据的人)。

The syntax you use show you are uncomfortable with the use of escaping : 您使用的语法表明您对使用转义感到不舒服:

this 这个

{% autoescape on %}
    {{ content }}
{% endautoescape %}

is exactly the same as this 与此完全一样

{{ content|escape }}

this 这个

{{ content }}

is exactly the same as this <-- edit : If the autoescape is OFF (thanks to Paulo Scardine) 与此< - 编辑完全相同:如果autoescape为OFF(感谢Paulo Scardine)

{{ content|safe }} 

Safe is use like that : 安全是这样使用的:

{% autoescape on %}
    {{ content }}  <-- escape
    {{ content|safe }}  <-- not escape
{% endautoescape %}

Your question shows you are a little confused about what escaping is. 你的问题表明你对逃避是什么有点困惑。

Escaping is turning non-safe characters - like HTML tags - into escaped versions so that malicious content such as script tags don't ruin your site. 转义是将非安全字符(如HTML标记)转换为转义版本,以便恶意内容(如脚本标记)不会破坏您的网站。 Django does this by default on all content rendered in a template from a variable. Django默认在变量模板中呈现的所有内容上执行此操作。

It seems by your comment that you're the only one editing your content that what you want is to render your variables without the automatic escaping. 您的评论似乎是您唯一一个编辑内容的人,您想要的是在没有自动转义的情况下渲染变量。 So, for that, you need to mark it as safe. 因此,为此,您需要将其标记为安全。 You can either do this in the template, by either wrapping the whole lot in {% autoescape off %}...{% endautoescape %} tags or via the {{ myvar|safe }} filter on individual variables. 您可以在模板中执行此操作,方法是将整个数据包装在{% autoescape off %}...{% endautoescape %}标记中,或者通过{{ myvar|safe }}过滤器对各个变量进行{{ myvar|safe }} Or, you can do it in the view, by calling mark_safe(myvar) on individual variables before passing them to the template. 或者,您可以在视图中执行此操作,方法是在将各个变量传递给模板之前调用mark_safe(myvar)

To avoid escaping use "safe" ( https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#safe ): 为避免逃避使用“安全”( https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#safe ):

Marks a string as not requiring further HTML escaping prior to output. 将字符串标记为在输出之前不需要进一步的HTML转义。 When autoescaping is off, this filter has no effect. 自动关闭时,此过滤器无效。

To escape use "escape" ( https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#escape ): 要逃避使用“逃避”( https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#escape ):

Escapes a string's HTML. 转义字符串的HTML。

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

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