Django url在多行中使用模板中的参数

时间:2018-03-06 11:34:07

标签: django django-templates django-urls

我的模板中有这种类型的网址:

<a href="{% url 'company-detail' region=ownership.company.city.province.region.slug province=ownership.company.city.province.slug city=ownership.company.city.slug company=ownership.company.slug %}">

也就是说,正如你所看到的那样,长得很厉害。

有多种方法可以在多行中做同样的事情?

这样的事情:

<a href="{% url 'company-detail'
            region=ownership.company.city.province.region.slug
            province=ownership.company.city.province.slug
            city=ownership.company.city.slug
            company=ownership.company.slug %}">

1 个答案:

答案 0 :(得分:1)

{% with region=ownership.company.city.province.region.slug province=ownership.company.city.province.slug city=ownership.company.city.slug company=ownership.company.slug %}
<a href="{% url 'company-detail' region province city company %}">
{% endwith %}

你可以这样做