2016-02-27 51 views
0

所以在我的主頁中我根據用戶是否登錄而擴展了2個東西。然後,如果他們試圖登錄,並且失敗了,我想投入到紅:那麼奇怪的程序塊/擴展Django中的交互

{% extends extendVar %} #in this case, extendvar=notLoggedIn.html 
    {% block signIn %} 
     {% if failure %} 
      <div class="form-group has-error"> 
       <input type="email" id="email" name="email" placeholder="Email" class="form-control"> 
      </div> 
      <div class="form-group has-error"> 
       <input type="password" name="password" placeholder="Password" class="form-control"> 
      </div> 
     {% endif %} 
    {% endblock %} 

notLoggedIn.html從標題頁包括:

<div class="container" style=""> 
    {% include 'header.html' %} 
</div> 

這有一個塊標籤:

<div class="header clearfix"> 
    <nav> 
    <ul class="nav nav-pills pull-left"> 
     <a class="" href="#"> 
     <img alt="Brand" src="{% static 'images/Logo.png' %}" style="height:auto; max-width:470px; margin-top:-22px; margin-bottom:-30px; padding-right:10px;"> 
     </a> 
    </ul> 

    <ul class="nav nav-pills pull-right"> 
     <div id="navbar" class="navbar-collapse collapse"> 
      <form class="navbar-form navbar-right" action="{% url 'signIn' %}" method="POST"> 
      {% csrf_token %} 
      {% block signIn %} 
      <div class="form-group"> 
       <input type="email" id="email" name="email" placeholder="Email" class="form-control"> 
      </div> 
      <div class="form-group"> 
       <input type="password" name="password" placeholder="Password" class="form-control"> 
      </div> 
        <button class="btn btn-primary btn" href="" role="button" style="">Sign in </button> 
      {% endblock %} 
      </form> 
     </div> 
    </ul> 
    </nav> 

項目名稱</H3> - >

我唯一的問題是,因爲我在NotLoggedIn中包含標題,然後擴展它,它不保留塊標記,因此signIn塊不起作用。如果不是做包含頭文件,我只是硬編碼,塊完美地工作。有任何想法嗎?

回答

0

根據django文檔,當您使用「include」時,包含模板中的塊首先被評估,然後被替換。因此,在您的模板中,不能覆蓋包含模板的塊。

https://docs.djangoproject.com/es/1.9/ref/templates/builtins/#include

而不是使用一個塊,如果你傳遞變量下到包括塊,讓它呈現你怎麼想?例如:

您的模板:

{% include 'header.html' with failure=failure %} 

了header.html:

<div class="form-group {% if failure %}has-error{% endif %}"> 
    <input type="email" id="email" name="email" placeholder="Email" class="form-control"> 
</div> 
<div class="form-group {% if failure %}has-error{% endif %}"> 
    <input type="password" name="password" placeholder="Password" class="form-control"> 
</div>