preventing template from converting html entities

536 views
Skip to first unread message

Federico Capoano

unread,
Feb 24, 2010, 9:36:31 AM2/24/10
to Django users
Hello to all,

simple question:

I have the following HTML in a template:
<a href="#" title="More details on &quot;{{ villa.name }}&quot;
class="tooltip">

But it gets rendered this way:
<a href="#" title="More details on "{{ villa.name }}" class="tooltip">

That is, the &quot; entity is converted to the respective character,
". Very nice, but i'd need &quot;

Is there a filter or something i can use to tell the Django Template
System to render &quot; ?

Thanks in advance.

Federico


Daniel Roseman

unread,
Feb 24, 2010, 9:56:09 AM2/24/10
to Django users

If I've understood you correctly, it's not the templating system that
is converting it, but the browser. When a browser sees &quot; it will
print ".

To prevent this, you'll need to double-escape it - ie replace the &
with the encoded version, so you have &amp;quot;
--
DR.

Alex Robbins

unread,
Feb 25, 2010, 9:33:20 AM2/25/10
to Django users
I think you can do that with title={% filter force_escape
%}"{{ villa.name }}"{% endfilter %}. Haven't tried it though.

Alex

Alex Robbins

unread,
Feb 25, 2010, 9:35:26 AM2/25/10
to Django users
Oops, it would actually be
title={% filter force_escape %}&quot;{{ villa.name }}&quot;{% endfilter %}

Sorry.
--
Alex Robbins
5Q Communications, Inc.
http://www.5Qcommunications.com/
alex.r...@5Qcommunications.com
800-747-4214 ext 913 (p)
http://www.ask5q.com/twitter/

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

Federico Capoano

unread,
Mar 11, 2010, 12:26:14 PM3/11/10
to Django users
Hey sorry guys, i couldn't find this post anymore!

Thanks for the answers but i've found the resolution to my problem in
the documentation, i just forgot in the beginning where it says about

{% autoescape off %}

This is the tag to use to prevent the template system from converting

&quot;

in

"

Example:

<a href="#" title="I want &quot;this words to be appear in a
javascript tooltip and be quoted&quot;" class="tooltip">Test</a>

Becomes

<a href="#" title="I want "this words to be appear in a javascript
tooltip and be quoted"" class="tooltip">Test</a>

Causing serious problems on some browsers, cos the html is not valid.

While

{% autoescape off %}<a href="#" title="I want &quot;this words to be
appear in a javascript tooltip and be quoted&quot;"
class="tooltip">Test</a>{% endautoescape %}

will output correctly

<a href="#" title="I want &quot;this words to be appear in a
javascript tooltip and be quoted&quot;" class="tooltip">Test</a>.

Didn't try force_escape, but that sounds the opposite of what I want
to do..

Thanks.

PS: Django is great, no doubt the best framework I ever used.


On Feb 25, 3:35 pm, Alex Robbins <alexander.j.robb...@gmail.com>
wrote:


> Oops, it would actually be
> title={% filter force_escape %}&quot;{{ villa.name }}&quot;{% endfilter %}
>
> Sorry.
> --
> Alex Robbins
> 5Q Communications, Inc.http://www.5Qcommunications.com/

> alex.robb...@5Qcommunications.com
> 800-747-4214 ext 913 (p)http://www.ask5q.com/twitter/

Reply all
Reply to author
Forward
0 new messages