如何在 django 模板中放置大括号?
我需要生成一个用大括号括起来的 id(例如“{1234}”)。使用django模板语言,大括号也用于启动变量替换,所以我在获得我想要的东西时遇到了一些麻烦。我尝试过
{{{ id }}}
{{ '{'id'}' }}
{{ '{'+id+'}' }}
{ {{ id }} }
这些方法都不起作用,除了最后一个,不幸的是它产生了“{1234}”,这不是我想要的。我目前有两种解决方案:要么传递一个已经包含 {} 的 id 变量(丑陋),要么编写一个自定义过滤器,然后编写 {{ id|add_braces }} (我更喜欢它)。
在这样做之前,我更愿意问是否存在更好的解决方案。
使用转义值不起作用。即使我添加 {% autoescape off %}%7B{% endautoescape %} 我也没有得到 {,这很奇怪,但这是另一个问题。
谢谢
编辑:我写了一个快速过滤器。将其粘贴到此处,以便其他人可以将其用作编写更复杂的模板的模板。放入python包application_path/templatetags/formatting.py
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def add_braces(value):
return "{"+value+"}"
I need to produce an id surrounded by braces ( for example "{1234}" ). With the django template language, braces are also used to start a variable substitution, so I have some trouble in obtaining what I want. I tried
{{{ id }}}
{{ '{'id'}' }}
{{ '{'+id+'}' }}
{ {{ id }} }
None of these methods work, except the last one, which unfortunately produces "{ 1234 }", not what I want. I currently have two solutions : either I pass an id variable already containing the {} (ugly) or I write a custom filter and then write {{ id|add_braces }} (I prefer it).
Before going this way, I prefer to ask if a better solution exists.
Using escaped values does not work. Even if I add {% autoescape off %}%7B{% endautoescape %} I don't get the {, which is strange, but that's another problem.
Thanks
Edit: I wrote a quick filter. Pasting it here so someone else can use it as a template for writing a more complex one. To be put into python package application_path/templatetags/formatting.py
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def add_braces(value):
return "{"+value+"}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你的答案可以在这里找到:
http://docs.djangoproject .com/en/dev/ref/templates/builtins/#templatetag
简而言之,您想要使用
{% templatetag openbrace %}
和{% templatetag closebrace %}< /代码>。
编辑:
Django 现在还包含此开箱即用的功能:
I think your answer can be found here:
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag
In short, you want to use
{% templatetag openbrace %}
and{% templatetag closebrace %}
.Edit:
Django now also includes this functionality out of the box:
{% templatetag openbrace %}
变得极其冗长,例如 javascript 模板我使用了 这个要点为此目的取得了一些成功,它可以让你做类似的事情
{% templatetag openbrace %}
become extremely verbose for e.g. javascript templatesI've used the
verbatim
tag from this gist with some success for exactly this purpose which lets you do something likeJinja 模板语言的建议也适用于 Django 模板引擎:
http:// jinja.pocoo.org/docs/dev/templates/#escaping
解决方案是这样的:
当然,其他两个答案也有效,但在我看来,这是一个不太冗长且更具可读性的答案。
The recommendation from the Jinja templating language works with the Django templating engine as well:
http://jinja.pocoo.org/docs/dev/templates/#escaping
The solution is this:
Of course the other two answers work, but this is one is less verbose and more readable, in my opinion.