2012-02-17 39 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