"&" string to template adds "&amp" in the html

22 views
Skip to first unread message

dk

unread,
May 21, 2015, 12:35:46 PM5/21/15
to django...@googlegroups.com
I am creating a string inside the view that will be use in the template on the javascript.
my string in python contains &   and I print the view and works fine,  but when is send to the template I don get &amp.

is there something magical about &   ?  or any hint on how to make it work?  thanks guys.

Andreas Kuhne

unread,
May 21, 2015, 12:53:07 PM5/21/15
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a432f1b0-5042-4b4a-a7ff-57e92ce539be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yes &amp; is & when it is HTML encoded. You will get the same problem if you try to send "<b>Bold text</b>" to a template, it will become the HTML equivalent. All strings sent from your view function / class to the template will be HTML encoded UNLESS you use safestring strings: https://docs.djangoproject.com/en/1.8/ref/utils/#module-django.utils.safestring

Regards,

Andréas

dk

unread,
May 22, 2015, 2:11:27 PM5/22/15
to django...@googlegroups.com
I fixed doing this

{% autoescape off %}
     {{ my_var_with_& }}
{% endautoescape%}

now works fine.

Javier Guerra Giraldez

unread,
May 22, 2015, 3:15:25 PM5/22/15
to django...@googlegroups.com
On Fri, May 22, 2015 at 1:11 PM, dk <dem...@gmail.com> wrote:
> I fixed doing this
>
> {% autoescape off %}
> {{ my_var_with_& }}
> {% endautoescape%}


the "right" way is:

{{ my_var_with_anything|safe }}

--
Javier
Reply all
Reply to author
Forward
0 new messages