3

我创建了 2 个导航栏,一个用于博客,一个用于主应用程序。我使用包含标签将导航栏引入模板。“extends base.html”也被使用,但是导航栏不包含在基本文件中,如上所述作为包含标签引入。

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

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

我认为导航栏将尝试仅加载活动(当前打开的)html页面的视图函数的上下文变量,因此主页上下文变量在任何其他页面上都不起作用,即使导航栏是一个单独的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%}

同样,其余页面同时介绍了基本和导航栏。

例如,导航栏 {% for country in countries %} 中使用的所有上下文变量都来自主页的视图函数。

当我使用相同的导航栏说“关于我们”页面时,导航栏不会显示所需的信息。

这有帮助吗?

4

1 回答 1

0

你可以做的简单方法

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 在每个模板中都进行了扩展,因此您可以在所有模板中访问该上下文变量

于 2015-01-08T06:00:46.380 回答