python - 在 Django 模板中包含 url 变量

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

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

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

我试过了

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

最佳答案

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

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

文档链接 here .

关于python - 在 Django 模板中包含 url 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29650187/

相关文章:

python - Python 的 AoT 编译器

Python - 输入用户输入的天数,然后显示等效的年、月和日

python - 如何使 Python 子类不可调用

python - 在 django 模型中自动生成席位

python - Django:过滤包含存储正则表达式的字段的模型

Python 网页抓取被阻止

Django MPTT Postgres 更新查询运行缓慢

django - 在Django项目中设置Jinja2

html - Django trans 标签可以包含 HTML 标签吗?

python - Django:外键数据的总值(value)