0
我正在將我的舊項目升級到最新版本的python/django,並且在自定義模板標記中遇到問題。防止在django中自動轉義自定義模板標記
模板標籤定義:
from django import template
register = template.Library()
def my_tag(*args) -> str:
""" returns html code """
register.simple_tag(lambda *x: my_tag("hello world", *x), name='my_tag')
例標籤用法:
{% my_tag "this no longer works, this autoescapes my code" %}
如何修改我的標籤定義,以防止autoescaping,這樣我就不必修改模板:
{% autoescape off %}{% my_tag "workaround, this doesn't autoescape html" %}{% endautoescape %}