如何在 django 模板中放置大括号?

发布于 2024-08-09 20:30:05 字数 784 浏览 6 评论 0原文

我需要生成一个用大括号括起来的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烈酒灼喉 2024-08-16 20:30:05

我想你的答案可以在这里找到:

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

简而言之,您想要使用 {% templatetag openbrace %}{% templatetag closebrace %}< /代码>。

编辑:
Django 现在还包含此开箱即用的功能:

{% verbatim %} {{ blah blah }} {% endverbatim %}

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:

{% verbatim %} {{ blah blah }} {% endverbatim %}
天涯离梦残月幽梦 2024-08-16 20:30:05

{% templatetag openbrace %} 变得极其冗长,例如 javascript 模板

我使用了 这个要点为此目的取得了一些成功,它可以让你做类似的事情

{{ request.user }}
{% verbatim %}
     brackets inside here are left alone, which is handy for e.g. jquery templates
     {{ this will be left }}
     {% so will this %}
{% endverbatim }}

{% more regular tags (to be replaced by the django template engine %}

{% templatetag openbrace %} become extremely verbose for e.g. javascript templates

I've used the verbatim tag from this gist with some success for exactly this purpose which lets you do something like

{{ request.user }}
{% verbatim %}
     brackets inside here are left alone, which is handy for e.g. jquery templates
     {{ this will be left }}
     {% so will this %}
{% endverbatim }}

{% more regular tags (to be replaced by the django template engine %}
策马西风 2024-08-16 20:30:05

Jinja 模板语言的建议也适用于 Django 模板引擎:

http:// jinja.pocoo.org/docs/dev/templates/#escaping

解决方案是这样的:

{{ '{' }}{{ id }}{{ '}' }}

当然,其他两个答案也有效,但在我看来,这是一个不太冗长且更具可读性的答案。

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:

{{ '{' }}{{ id }}{{ '}' }}

Of course the other two answers work, but this is one is less verbose and more readable, in my opinion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文