2012-02-17 37 views
0

也许我正在走这个所有错误的方式...但我想要做的是改变左侧导航我通过什么网页。 base.html中的左侧导航栏显而易见,但是一旦用户位于forums.html(扩展base.html)上,我想让左侧导航栏更改。在某个页面上更改为我的左侧导航栏?

base.html文件:

{% 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 %} 

我不知道有没有通过基本上下文或者不通过。我很感谢帮助和任何想法/建议。

回答

0

django {% block %}模板标签可让您定义可由子模板覆盖的内容块。尝试是这样的:

base.html文件

<div class="container"> 
    <div class="row"> 
     {% block nav %} 
      <nav> 
        <!-- original base.html nav goes here --> 
      </nav> 
     {% endblock %} 
    </div> 
</div> 

,然后在forums.html

{% extends "base.html" %} 

{% block nav %} 

    <nav> 
     <!-- new forums.html nav goes here --> 
    </nav> 

{% endblock %} 

输出看起来像

<div class="container"> 
    <div class="row"> 
      <nav> 
       <!-- new forums.html nav goes here --> 
      </nav> 
    </div> 
</div> 

文档在这里:https://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance

+0

辉煌,谢谢! – Modelesq 2012-02-17 17:54:54