Mark_safe not working / still executing in simple_tag

I am using Django 1.3 and I have simple_tag that I need to return some unescaped HTML, and no matter what I do, it still eludes mine and to amp; and mine | up to% 7C.

from django import template from django.template import loader, Context from django.utils.safestring import mark_safe register = template.Library() @register.simple_tag() def show_stuff(arg1=None, arg2=None, arg3='someconstant'): # Do some stuff if someconstant == 'somevalue': template_name = 'template1.html' else: template_name = 'template2.html' template = loader.get_template(template_name) html = template.render(Context( {'myvar': myvar,} )) html = mark_safe(html) print type(html) return html 

Print statement shows

 <class 'django.utils.safestring.SafeUnicode'> 

which from my understanding should not be escaped. I call the tag in my template like this:

 {% show_stuff 'arg1' 'arg2' 'arg3' %} 

Help would be greatly appreciated.

Update: I tried the following from the comments below. Did not return an error, but still avoids HTML:

  ... template = loader.get_template(template_name) html = template.render(Context( {'myvar': myvar,} )) html = mark_safe(html) print type(html) return html show_stuff().is_safe=True 

Also tried wrapping the contents of template1.html and template2.html in

 {% autoescape off %} template html contents with & and | {% endautoescape %} 

and wrapping the templatetag call itself with autoescape tags. No success.

+4
source share
1 answer

In a discussion with the author of the question, we found that the characters were not actually escaped. Scoopseven (author) looked at the source html with the "Show Selection Source" option in the context menu in Firefox. In this case, escaped characters are displayed.

+2
source

All Articles