Django 在某个页面上更改为我的左导航栏?
也许我走错了方向。。。但我要做的是改变左边的导航,我在哪个页面上。base.html中的左导航在整个过程中都很明显,但是一旦用户访问了forums.html(extensed base.html),我就想更改左导航 base.html:Django 在某个页面上更改为我的左导航栏?,django,html,django-templates,django-context,Django,Html,Django Templates,Django Context,也许我走错了方向。。。但我要做的是改变左边的导航,我在哪个页面上。base.html中的左导航在整个过程中都很明显,但是一旦用户访问了forums.html(extensed base.html),我就想更改左导航 base.html: {% if not no_left_bar %} <div class="container"> <div class="row"> <!-- Left Side Bar -->
{% if not no_left_bar %}
<div class="container">
<div class="row">
<!-- Left Side Bar -->
<nav>
original base.html nav goes here
</nav>
<!-- Some condition that goes here/ When forums.html -->
<nav>
forums.html( extends base.html ) nav goes here
</nav>
</div>
</div>
{% endif %}
{%if not no_left_bar%}
原始base.html导航位于此处
html(extends base.html)导航到这里
{%endif%}
我不知道是否必须通过基本上下文传递它。我感谢您的帮助和任何想法/建议。django
{%block%}
模板标记允许您定义可由子模板覆盖的内容块。试着这样做:
base.html
<div class="container">
<div class="row">
{% block nav %}
<nav>
<!-- original base.html nav goes here -->
</nav>
{% endblock %}
</div>
</div>
{% extends "base.html" %}
{% block nav %}
<nav>
<!-- new forums.html nav goes here -->
</nav>
{% endblock %}
{%block nav%}
{%endblock%}
然后在forums.html中
<div class="container">
<div class="row">
{% block nav %}
<nav>
<!-- original base.html nav goes here -->
</nav>
{% endblock %}
</div>
</div>
{% extends "base.html" %}
{% block nav %}
<nav>
<!-- new forums.html nav goes here -->
</nav>
{% endblock %}
{%extends“base.html”%}
{%block nav%}
{%endblock%}
输出将如下所示
<div class="container">
<div class="row">
<nav>
<!-- new forums.html nav goes here -->
</nav>
</div>
</div>
此处的文档: