2015-04-15 164 views
2

我在模板文件中有一个“创建新对象”按钮。在Django模板中包含url变量

我想包括按钮在我的网站上的几个地方,但我想包括一个链接模板。

我已经试过

{% include "snippets/icon_add.html" with link="/create_new_item/" only %} 

,但我想从{% url 'create_new_item' %}使用效益。

我可以这样做

{% include "snippets/icon_add.html" with link={% url 'create_new_item' %} only %} 

回答

6

{% url ... as var %}语法尝试。示例:

{% url 'create_new_item' as the_url %} 
{% include "snippets/icon_add.html" with link=the_url %} 

文档链接here

+2

用'{%url'create_new_item'作为the_url%}'不会抛出错误如果URL不存在。 –