Easy Way to Escape Django Template Variables

后端 未结 6 894
礼貌的吻别
礼貌的吻别 2021-02-04 06:28

For a new project we\'re writing documentation about the Django template system. We use Django for the documentation project itself too, so Django picks up all our exam

6条回答
  •  别跟我提以往
    2021-02-04 06:52

    I solved this by adding an "include_raw" template tag that behaves like the built-in "include" tag, but just doesn't parse or process the file passed to it. I'm running Django 1.2 under App Engine.

    Create a tags module (tags.py):

    from django.template import loader
    from google.appengine.ext.webapp import template
    
    register = template.create_template_register()
    
    @register.simple_tag
    def include_raw(path):
      return loader.find_template(path)[0]
    

    Register it:

    from google.appengine.ext.webapp import template
    
    template.register_template_library("tags")
    

    Use it:

    {% include_raw "this-will-be-included-verbatim.html" %}
    

提交回复
热议问题