【问题标题】:Why do balanced Django templating {% if %} and {% endif %} get Invalid block tag on line 50: 'endif', expected 'empty' or 'endfor'为什么平衡的 Django 模板 {% if %} 和 {% endif %} 在第 50 行得到 Invalid block tag: 'endif', expected 'empty' or 'endfor'
【发布时间】:2017-03-16 14:34:41
【问题描述】:

我收到一条错误消息,指出不应使用外部 {{ endif }} 关闭标签; {{ empty }}{{ endfor }} 是预期的。这听起来像我有一个 {{ endif }} 与早期的 {% if ... %} 不匹配,它希望我以嵌套方式关闭最外层循环(来源 http://pastebin.com/Knsbi6bH)。 (这是为了在 {{ for }} 循环内,但 {{ endif }} AFAIK 匹配开口 {{ if ... }}。它是 {{ endif }}</h2> 之前。

报告错误是第 50 行:

40                                          {% else %}
41                                              {% if pet.shelter.website or
42                                                pet.shelter.slugline %}
43                                                  this shelter
44                                              {% endif %}
45                                          {% endif %}
46                                          {% if pet.shelter.name or
47                                            pet.shelter.website %}
48                                              </a>)
49                                          {% endif %}
50                                      {% endif %}
51                                  </h2>
52                                  {% if pet.snippet %}
53                                      {{ pet.snippet }}
54                                  {% endif %}
55                              </td>
56                          </tr>
57                      {% endif %}
58                  {% endfor %}
59              </table>
60          {% else %}

Pastebin 在http://pastebin.com/Knsbi6bH

{% ... %} 标签是否需要在一行中?我欢迎任何关于我所缺少的想法。

【问题讨论】:

  • 请注意,您在整个问题中使用了{{}},而不是{%%}

标签: django django-templates


【解决方案1】:

是的,Django 模板标签必须在一行

请参阅例如ticket #8652(作为 wontfix 关闭)或 django-developers 邮件列表上的this discussion

您需要更改标签,如

{% if pet.shelter.name or 
    pet.shelter.website %}

{% if pet.shelter.name or pet.shelter.website %}

【讨论】:

    猜你喜欢
    • 2020-02-04
    • 2015-07-08
    • 2014-10-04
    • 2013-10-01
    • 2011-09-27
    • 1970-01-01
    • 2023-03-13
    • 2016-12-20
    • 2010-10-08
    相关资源
    最近更新 更多