django navbar模板 - 标签

时间:2015-01-07 07:13:52

标签: django django-templates django-context

我为博客创建了2个导航栏,为主应用创建了一个导航栏。我使用include标签将navbar引入模板。 "扩展base.html"但是,如果使用navbar,则navbar不包含在基本文件中,如上所述,它是作为include标记引入的。

在应用程序的所有页面应该呈现相同导航栏(内容)的主应用程序中没有这样做,我只在主页上收到所需的结果。

导航栏上使用的上下文变量来自主页视图功能,当呈现主页页面时,它可以很好地工作,但是当我打开同一应用程序的任何内部页面时,导航栏不会显示任何内容。

我认为navbar会尝试加载活动(当前打开的)html页面的视图函数的上下文变量,因此主页上下文变量不适用于任何其他页面,即使navbar是一个单独的html文件。

如果这是正确的,我想知道如何解决它,因为主应用程序上的所有页面都应显示相同的导航栏值。

home.html - This is how base and navbar are introduced in the template. 
{% extends 'base.html' %}

{% block title %}Title{% endblock title %}
{% block navbar %}{% include 'navbar.html' %}{% endblock navbar%}

同样,其余页面同时引入了base和navbar。

导航栏中使用的所有上下文变量{%in country in country%}例如来自主页的视图功能。

当我使用相同的导航栏说'&34;关于我们"页面导航栏未显示所需信息。

这有帮助吗?

1 个答案:

答案 0 :(得分:1)

你可以做的简单方法

in base.html

    {% block content %}
    {% endblock %}

    in navbar2.html file
    all html code


in navbar2.html file
        all html code



    other1.html file
    {% extends 'base.html'%}

    {% blck content %}
    {% include 'navbar1.html' %}


    All html code here
    {% endblock %}

other2.html file
        {% extends 'base.html'%}
        {% blck content %}

        {% include 'navbar1.html' %}

        All html code here
        {% endblock %}

如果要将上下文变量发送到所有文件,则创建视图,该视图将呈现给base.html并将该上下文变量传递给base.html。由于base.html在每个模板中都有扩展,因此您可以访问所有模板中的上下文变量