我已经创建了2个导航栏,一个用于博客,一个用于主应用程序。我使用include标签将导航栏引入模板中。“扩展base.html”也被使用,但是,导航栏不包括在基本文件中,并且如上所述作为包含标记引入。
在主应用程序中,应该为应用程序的所有页面呈现相同的导航栏(内容),而不是这样做,并且我只在主页上收到所需的结果。
导航栏上使用的上下文变量来自主页视图功能,当主页页面呈现时,它可以完美地工作,但是当我打开同一个应用程序的任何内部页面时,导航栏不会显示任何内容。
我假设navbar只会尝试加载活动的(当前打开的) html页面的view函数的上下文变量,因此主页上下文变量在任何其他页面上都不起作用,即使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。
在导航栏{% for country in countries %}中使用的所有上下文变量都来自主页的视图函数。
当我在“关于我们”页面中使用相同的导航栏时,导航栏不会显示所需的信息。
这有帮助吗?
发布于 2015-01-08 14:00:46
你可以做到的简单方法
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在每个模板中都进行了扩展,因此您可以在所有模板中访问该上下文变量
https://stackoverflow.com/questions/27814207
复制相似问题