2017-09-08 5 views
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" %} 

{% autoescape off %}{% my_tag "workaround, this doesn't autoescape html" %}{% endautoescape %} 

答えて

1

mark_safeメソッドで結果セーフをマークすることができます。

from django.utils.html import mark_safe 
def my_tag(*args) -> str: 
    return mark_safe(result) 
関連する問題