[英]Jinja2 “as” tag not working?
I have this code in index.html
: 我在
index.html
有以下代码:
{% url 'cronjobs:remove-job' as remove_job_url %}
And I want to use it further down in the same HTML file multiple times, eg: 我想在同一HTML文件中多次使用它,例如:
<a href="{{ remove_job_url }}">Remove job</a>
According to this answer , I think I got everything right. 根据这个答案 ,我认为我做对了所有事情。 However, the variable
remove_job_url
is null. 但是,变量
remove_job_url
为null。 Nothing is printed to the anchor tag. 锚标签上没有任何内容。
I have loaded the Jinja2 into my settings.py -> TEMPLATES
as described in the documentation , yet it does not seem to work. 我已经按照文档中的描述将Jinja2加载到我的
settings.py -> TEMPLATES
中 ,但它似乎无法正常工作。 Any ideas what might be wrong? 任何想法可能有什么问题吗?
(this also happens when I use the {% trans ... as trans_var %}
tag, the trans_var
is again - empty) (当我使用
{% trans ... as trans_var %}
标签时,也会发生这种情况, trans_var
再次trans_var
-空)
You are trying to use Django template syntax in a Jinja2 template. 您正在尝试在Jinja2模板中使用Django模板语法。 The template syntax is related, but no the same .
模板语法是相关的, 但是不相同 。 You need to use Jinja2 assignment syntax instead.
您需要使用Jinja2赋值语法 。
{% set remove_job_url = url('cronjobs:remove-job') %}
Note the url()
call . 注意
url()
调用 。 See the Url reversing section of the Django-jinja integration documentation. 请参阅Django-jinja集成文档的“ 网址反转”部分 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.