How do I prevent Django from interpreting a block which contains curly quotes?

隐身守侯 提交于 2019-12-23 19:13:30

问题


I'm working on a website which uses Django templates, and I have to produce HTML which contains paired curly quotes. Is there any way to disable Django's tag processing for a block of code? Something like a literal block tag would be nice, so that:

{% literal %}
 {% LITERAL {{ BRACES }}
{% endliteral %}

... would produce {% LITERAL {{ BRACES }} in the output.


回答1:


EDIT: Your syntax is currently impossible with the current lexer / parser system.

Why? Basically the template system has a Lexer and a Parser. The Lexer takes the template string as input, and tokenizes it. The parser then takes the list of tokens in its constructor and parses them into a list of a bunch of Nodes for the compiled template. The template tags and filters only have access to the already constructed parser -- you can't access the initial lexer string. See the comments in django/templates/__init__.py

However, there is a solution. It's not mine (see below), but its to basically use server side includes {% ssi some_file.html %} to include an extra file that has the literal text. Yes this is an ugly solution; but without a major rewrite of the templating system it will have to suffice.

Easy Way to Escape Django Template Variables




回答2:


For the record, this is possible now with the template tag verbatim.




回答3:


https://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag



来源:https://stackoverflow.com/questions/6837071/how-do-i-prevent-django-from-interpreting-a-block-which-contains-curly-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!