在Django模板中包含url变量

Jam*_*een 6 python django django-templates django-template-filters

我在模板文件中有一个"创建新对象"按钮.

我想在我的网站上包含几个位置的按钮,但我希​​望包含带有链接的模板.

我试过了

{% include "snippets/icon_add.html" with link="/create_new_item/" only %}
Run Code Online (Sandbox Code Playgroud)

但我想利用它的好处{% url 'create_new_item' %}.

我能做点什么吗

{% include "snippets/icon_add.html" with link={% url 'create_new_item' %} only %}
Run Code Online (Sandbox Code Playgroud)

Jos*_*mit 12

尝试使用{% url ... as var %}语法.例:

{% url 'create_new_item' as the_url %}
{% include "snippets/icon_add.html" with link=the_url %}
Run Code Online (Sandbox Code Playgroud)

文档链接在这里.

  • 如果url不存在,那么`{%url'create_new_item'为the_url%}`的catch不会抛出错误. (2认同)