Django模板语法错误

Raj*_*eev 4 django django-templates django-models django-views

以下代码中的语法是否有任何问题,有一个错误 Invalid block tag: 'else'

{% ifequal chat_profile 1 %}
    {% extends "chatprofile/chat_profile1.html" %}
{% else %}
    {% extends "chatprofile/chat_profile.html" %}
{% endifequal %}
Run Code Online (Sandbox Code Playgroud)

Car*_*bés 6

是的,您必须使用extends作为第一个标记,但您也可以将变量传递给它而不是固定字符串:

{% extends base %}
Run Code Online (Sandbox Code Playgroud)

然后,您可以包含一个以base要继承的模板名称命名的上下文变量,例如:

    return render_to_response('my_template.html',
                          { 'base': 'chatprofile/chat_profile1.html' })
Run Code Online (Sandbox Code Playgroud)