Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

Thanks

EDIT: Which filter should I use to have special characters converted to html entities automatically?

EDIT 2: I just realized that autoescape is not a valid filter.

like image 616
user14412 Avatar asked Jul 03 '12 07:07

user14412


People also ask

How do I escape a Django template?

Since the template system has no concept of "escaping", to display one of the bits used in template tags, you must use the {% templatetag %} tag.

How do you turn off Django's automatic HTML escaping?

For example, you can check if my_textfield contains a script tag. If so, mark the instance as malicious and return an escaped version of my_textfield (the normal Django behavior). Otherwise, use mark_safe to return your HTML code marked as safe.

What is Forloop counter in Django?

Django for loop counter All the variables related to the counter are listed below. forloop. counter: By using this, the iteration of the loop starts from index 1. forloop. counter0: By using this, the iteration of the loop starts from index 0.

What is Autoescape in Django?

autoescape. Controls the current auto-escaping behavior. This tag takes either on or off as an argument and that determines whether auto-escaping is in effect inside the block. The block is closed with an endautoescape ending tag.


2 Answers

HTML escaping is on by default in Django templates.

Autoescape is a tag. not a filter:

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

The 'escape' filter escapes a string's HTML. Specifically, it makes these replacements:

  • < is converted to &lt;
  • > is converted to &gt;
  • ' (single quote) is converted to &#39;
  • " (double quote) is converted to &quot;
  • & is converted to &amp;

The 'force_escape' is almost identical to 'escape' except for a few corner cases.

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?

Well, you mean, like converting à to &Atilde;? Stick with utf-8 encoding all the way and forget about those.

like image 176
Paulo Scardine Avatar answered Oct 05 '22 22:10

Paulo Scardine


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)

{{ content|safe }}  

Safe is use like that :

{% autoescape on %}     {{ content }}  <-- escape     {{ content|safe }}  <-- not escape {% endautoescape %} 
like image 23
BlueMagma Avatar answered Oct 06 '22 00:10

BlueMagma