简化调用如何在Django模板中包含多个viariables?

时间:2015-09-04 22:23:43

标签: django django-templates

我想简化(更易读)Django模板代码:

{% comment %}
required variables:
group_id = 'exclude-brands-group'
select_name = 'exclude-brands'
entities = 'excludeBrandsSets'
add_keyword_name = {% trans 'Brand' %}
url_edit_keywords = {{ url_project_filter_exclude_brands_edit_keywords }}
{% endcomment %}

{% with group_id='exclude-keywords-group' select_name='exclude-keywords' %}
{% with entities=excludeKeywordsSets %}
{% trans 'Words' as add_keyword_name %}
{% with url_edit_keywords=url_project_filter_exclude_keywords %}
{% include 'web_site/seo/frontend/seo/filtered_keyword_idea/template/keyword_filter_group.html' %}
{% endwith %}
{% endwith %}
{% endwith %}

但我不知道如何使用标签减少的数量 - 因为 include 只是一行命令。我不知道如何简化trans。您是否知道将变量传递给模板的更简单方法?

1 个答案:

答案 0 :(得分:2)

include tag允许您将其他上下文传递给模板。这意味着你不需要与标签分开(尽管缺点是你最终会有很长的线条)。没有任何方法可以包含反式标记。

{% trans 'Words' as add_keyword_name %}
{% include 'web_site/seo/frontend/seo/filtered_keyword_idea/template/keyword_filter_group.html' with group_id='exclude-keywords-group' select_name='exclude-keywords' entities=excludeKeywordsSets url_edit_keywords=url_project_filter_exclude_keywords %}