Я использую Django 1.3, и у меня есть простой тег _, который мне нужен, чтобы вернуть некоторый -экранированный HTML, и независимо от того, что я делаю, он все еще экранирует мой & в & и мой | до %7С.
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 тегами autoescape. Нет успеха.