【问题标题】:I don't understand this Django template error message我不明白这个 Django 模板错误消息
【发布时间】:2011-12-19 21:10:54
【问题描述】:

这是填充我的页面的错误消息,附带代码问题:

/home 处的模板语法错误

无效的块标签:'endblock',应为'endif'

请求方法:GET

请求网址:http://localhost:8000/home

Django 版本:1.3.1

异常类型:TemplateSyntaxError

异常值:无效块标签:'endblock',预期为'endif'

代码:

{% extends "profile/base_with_classes.html" %}

{% block username %}
    {% if user == None %}
        Nobody
        {% else %}
            {% if user.is_authenticated %}
            {{ user.username }}
        {% else %}
            Unauthenticated User
        {% endif %}
    (% endif %}
{% endblock username %}    <==== Issue is here

我不明白。这是模板的顶部。

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    在最后一个 endif 中,你有 ( 而不是 {

    【讨论】:

    • +1 好收获。这绝对是问题所在。该错误表明在使用{% endif %} 关闭if 语句之前调用了{% endblock %}。这是因为{% endif %} 格式错误。
    【解决方案2】:

    嗯,这不仅仅是问题; {% endblock %} 标签实际上并不需要关闭块的名称。尝试在登录时和未登录时尝试各种情况

    【讨论】:

    • 它可能不需要它,但包含名称不是问题,而且它确实使文件更具可读性,尤其是在嵌套块或非常长的块的情况下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    相关资源
    最近更新 更多