【问题标题】:Error with Template Tags: Invalid Block Tags模板标签错误:无效的块标签
【发布时间】:2013-09-01 23:38:29
【问题描述】:

与 CreateView 相比,我正在尝试为 UpdateView 使用稍微不同的输出模板,并且我正在使用此模板代码删除 UpdateView 中的密码字段

我的模板代码如下:

{% for field in form %}
<div id="{{ field.auto_id }}_container">
{{ field.help_text }}
<div>
{% if customuser.id and field.label != 'Create your password' or
customuser.id and field.label != 'Confirm your password' %}

{{ field.label_tag }} {{ field }}

{% else %}

{{ field.label_tag }} {{ field }}

{% endif %}
</div>
<div id="{{ field.auto_id }}_errors">
{{ field.errors }}
</div>
</div>
{% endfor %}

我得到错误无效的块标签:'else',预期的'empty'或'endfor'。

感谢大家的帮助!

【问题讨论】:

  • 那是你的实际代码吗?看起来还不错。
  • 我在这里看不到 if..else 的意义。它在做同样的事情

标签: django django-templates


【解决方案1】:

如果您使用的是最近的 Django,https://github.com/django/django/commit/4aa97f5c18 引入了一个 {% empty %} 块,允许您编写

{% for athlete in athlete_list %}
    ...
 {% empty %}
   No athletes
{% endfor %}

当您想做的事情涉及迭代非空列表时很有用。

【讨论】:

    【解决方案2】:

    您不能在 {% if ... %} 模板标签(或任何标签,实际上)内换行。您应该将所有内容集中在一行中:

    {% if customuser.id and field.label != 'Create your password' or customuser.id and field.label != 'Confirm your password' %}
    

    【讨论】:

      猜你喜欢
      • 2011-02-09
      • 2017-06-20
      • 2010-11-25
      • 2011-03-04
      • 1970-01-01
      • 2011-10-05
      • 2012-09-10
      • 2013-10-08
      • 1970-01-01
      相关资源
      最近更新 更多