Използвам Django 1.3 и имам simple_tag, който трябва да върна някакъв неекраниран HTML и без значение какво правя, той все още избягва моите & към и моите | до %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
Изявлението за печат разкрива
<class 'django.utils.safestring.SafeUnicode'>
което според моите разбирания не трябва да бъде избягано. Извиквам етикета в моя шаблон така:
{% show_stuff 'arg1' 'arg2' 'arg3' %}
Помощта ще бъде високо оценена.
Актуализация: Опитах следното от коментарите по-долу. Не върна грешка, но все пак избягва 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
Също така опитах да опаковам съдържанието на template1.html и template2.html
{% autoescape off %}
template html contents with & and |
{% endautoescape %}
и обвиване на самото извикване на templatetag с тагове за автоматично излизане. Без успех.