I was testing Jinja2 in a Django project and have a strange output.
When I render the form, some characters are HTML encoded (< >
etc.)
In the template :
{{ form.as_p() }}
It renders to the browser :
<p><label for="id_username">Utilisateur:</label> <input autocomplete="off" id="id_username" type="text" name="username" maxlength="100" /></p> <p><label for="id_password">Mot de passe:</label> <input autocomplete="off" type="password" name="password" id="id_password" /></p>
Looking at sources :
&lt;p&gt;&lt;label for=&quot;id_username&quot;&gt;Utilisateur:&lt;/label&gt; &lt;input autocomplete=&quot;off&quot; id=&quot;id_username&quot; type=&quot;text&quot; name=&quot;username&quot; maxlength=&quot;100&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;label for=&quot;id_password&quot;&gt;Mot de passe:&lt;/label&gt; &lt;input autocomplete=&quot;off&quot; type=&quot;password&quot; name=&quot;password&quot; id=&quot;id_password&quot; /&gt;&lt;/p&gt;
Does anyone know this problem ?
Jinja2 is a modern day templating language for Python developers. It was made after Django's template. It is used to create HTML, XML or other markup formats that are returned to the user via an HTTP request.
The default Jinja delimiters are configured as follows: {% ... %} for Statements. {{ ... }} for Expressions to print to the template output. {# ... #} for Comments not included in the template output.
{{ form.as_p }} – Render Django Forms as paragraph. {{ form.as_ul }} – Render Django Forms as list.
Jinja2 tries to be safe by HTML-escaping the data. So you have to use |safe
filter.
Though I haven't used Django with Jinja2, I believe this should work:
{{ form.as_p()|safe }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With