gpt4 book ai didi

python - Django:如何在包含模板标签中正确传递URL?

转载 作者:行者123 更新时间:2023-11-30 22:23:49 26 4
gpt4 key购买 nike

在两个不同的模板中,我有两个几乎相同的 block ,只是 url 不同:

模板1.html

<div class="col-sm-4">
<a href="{% url 'bo:article-list' %}" class="btn btn-block btn-secondary btn-sm"
role="button">
<i class="fa fa-chevron-left"></i> Annuler
</a>
</div>

template2.html

<div class="col-sm-4">
<a href="{{ article.get_absolute_url }}" class="btn btn-block btn-secondary btn-sm"
role="button">
<i class="fa fa-chevron-left"></i> Annuler
</a>
</div>

我想让这个变得干燥,创建一个模板,然后进行包含。例如:

_cancel.html

<div class="col-sm-4">
<a href="{{ cancel_url }}" class="btn btn-block btn-secondary btn-sm"
role="button">
<i class="fa fa-chevron-left"></i> Annuler
</a>

对于 template2.html,它将适用于:

{% include 'includes/_cancel.html' with cancel_url=article.get_absolute_url %}

但是template1.html呢?这显然行不通:

{% include 'includes/_cancel.html' with cancel_url={% url 'bo:article-list' %} 

我猜有一个技巧。感谢您的帮助:)

最佳答案

url 标记可用于通过使用 as 将其结果保存到上下文变量中。 .

{% url 'bo:article-list' as cancel_url %}

您现在可以将其传递到您的包含标记中。

关于python - Django:如何在包含模板标签中正确传递URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47952896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com