How to escape {{ or }} in django template?

前端 未结 10 1917
慢半拍i
慢半拍i 2020-12-01 04:17

Django treats {{ var }} as some variable in its template. How can I escape {{ var }} or {{ or }} such that django does no

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 04:41

    if you simply need to use {{ }} as a variable for template framework like angularjs, then following maybe simpler:

    in your /templatetags/ngvar.py , add

    from django import template
    register = template.Library()
    
    @register.simple_tag
    def ngvar(var_name):
        return "{{%s}}" % var_name
    

    and in template, do

    {% load ngvar %}
    {% ngvar "variable name" %}
    

    if ngvar.py is the first template tag, then make sure to add __init__.py file to the templatetags directory

提交回复
热议问题