Django - 包含文件中的变量无法识别

时间:2016-06-18 01:31:26

标签: python django variables django-templates include

我正在使用djano 1.8.1和python 2.7。

在我的两个模板页面上,我有相同的变量集。

所以我决定创建一个包含这些变量的{% include %}页面,然后在所需页面上添加{% include %}标记以调用变量。

但是,当我设置包含文件并将变量剪切并粘贴到包含文件并将include标记添加到模板时,无法识别变量。

没有给出错误,不显示变量。我已检查{% include %}标记的路径是否正确。

我做错了什么?

以下是inlcude文件的内容:

{% load i18n %}

{% trans "Achievement Details" as var_details_name01 %}
{% trans "Type" as var_details_name05 %}
{% language "de" %}{% trans "Type" as var_details_name05_de %}{% endlanguage %}
{% language "es-419" %}{% trans "Type" as var_details_name05_es_419 %}{% endlanguage %}
{% language "en-CA" %}{% trans "Type" as var_details_name05_en_CA %}{% endlanguage %}
{% language "en-GB" %}{% trans "Type" as var_details_name05_en_GB %}{% endlanguage %}
{% language "fr-CA" %}{% trans "Type" as var_details_name05_fr_CA %}{% endlanguage %}
{% language "it" %}{% trans "Type" as var_details_name05_it %}{% endlanguage %}
{% trans "Achievement" as var_details_name06 %}
{% language "de" %}{% trans "Achievement" as var_details_name06_de %}{% endlanguage %}
{% language "es-419" %}{% trans "Achievement" as var_details_name06_es_419 %}{% endlanguage %}
{% language "en-CA" %}{% trans "Achievement" as var_details_name06_en_CA %}{% endlanguage %}
{% language "en-GB" %}{% trans "Achievement" as var_details_name06_en_GB %}{% endlanguage %}
{% language "fr-CA" %}{% trans "Achievement" as var_details_name06_fr_CA %}{% endlanguage %}
{% language "it" %}{% trans "Achievement" as var_details_name06_it %}{% endlanguage %}
{% trans "Achievement" as var_details_name07 %}
{% trans "Suggestion" as var_details_name04 %}

以下是包含我的模板文件中正确位置的包含标记:

{% block extra_head %}
    <script type="text/javascript" src="{{ STATIC_URL }}js/details_common.js"></script>
{% endblock %}

{% block page_content %}
    {% include "user_details/achievement_details_variables.html" %}

    {% include "headline/headline_with_toggle_help_icon.html" %}
    {% include "user_details/help_resume_details.html" %}
    ......
{% endblock %}

1 个答案:

答案 0 :(得分:2)

{% include %}标记的结果是html,这意味着您无法在包含的模板中定义变量(或块)。