奇怪的阻止/扩展Django中的交互

时间:2016-02-27 00:47:16

标签: python html css django django-templates

所以在我的主页中,我根据用户是否登录来扩展2件事。然后,如果他们已经尝试登录并失败,我希望输入为红色:

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

项目名称&lt; / h3&gt; - &GT;  

我唯一的问题是因为我在NotLoggedIn中包含头,然后扩展它,它不会保留块标记,因此signIn块无法正常工作。如果不是做包含头,我只是硬编码,块完美。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

根据django文档,当您使用“include”时,首先评估包含模板中的块,然后替换。在模板中,您不能覆盖包含模板的块。

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

如果将变量传递给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>