【问题标题】:Include with url variable in Django template在 Django 模板中包含 url 变量
【发布时间】:2015-06-21 09:52:42
【问题描述】:

我在模板文件中有一个“创建新对象”按钮。

我想在我的网站的几个位置包含按钮,但我想包含带有链接的模板。

我试过了

{% 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 %}

【问题讨论】:

    标签: python django django-templates django-template-filters


    【解决方案1】:

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

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

    文档链接here.

    【讨论】:

    • 如果 url 不存在,{% url 'create_new_item' as the_url %} 不会抛出错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2011-03-09
    相关资源
    最近更新 更多