我创建了 2 个导航栏,一个用于博客,一个用于主应用程序。我使用 include 标签将导航栏引入模板中。也使用了“extends base.html”,但是,导航栏不包含在基本文件中,并且如上所述作为包含标记引入。
在主应用程序中,应该为应用程序的所有页面呈现相同的导航栏(内容),但没有这样做,我只在主页上收到所需的结果。
导航栏上使用的上下文变量来自主页 View 功能,当呈现主页页面时,它可以完美工作,但是当我打开同一应用程序的任何内部页面时,导航栏不会显示任何内容。
我认为导航栏只会尝试加载事件(当前打开)html页面的 View 功能的上下文变量,因此主页上下文变量在任何其他页面上都不起作用,即使导航栏是一个单独的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 states %} 均来自主页的 View 函数。
当我使用相同的导航栏表示“关于我们”页面时,导航栏不会显示所需的信息。
这有帮助吗?
最佳答案
简单的方法即可实现
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 的 View ,并将该上下文变量传递到 base.html。由于 base.html 在每个模板中都进行了扩展,因此您将可以访问所有模板中的上下文变量
关于django 导航栏模板 - 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27814207/