0

可能重复:
Django 模板语法错误

当我在此代码上使用{% if request.user.is_authenticated %}条件进行重定向时,它会引发错误Invalid block tag: 'else'

{% if request.user.is_authenticated %}

{% extends "pages/page.html" %}
{% load mezzanine_tags shop_tags i18n %}
{% block body_id %}category{% endblock %}
{% block main %}{{ block.super }}
{% regroup products by category as products_by_category %}
{% for c in products_by_category %}
......  
         {% for p in c.list %}     
......
        {% if p.num_in_stock == None %}
...
         {% else %}
         {% if p.num_in_stock < 4 %}
...
            {% endif %}
            {% endif %}
        .....        
                   {% endfor %}
......
{% endfor %}
{% endblock %}

{% else %}

<script>
window.location="/stylequiz/";
</script>

如果我使用这个脚本,那么它不会出错

{% if request.user.is_authenticated %}
   <h1>welcome</h1>
{% else %}

<script>
window.location="/stylequiz/";
</script>
{% endif %}

我认为嵌套 if一定有问题。

4

1 回答 1

2

你不能把{%extends%}标签放在里面{%if%}。它应该是模板中的第一个标签。

来自 django docs模板继承

如果您在模板中使用 {% extends %},它必须是该模板中的第一个模板标签。否则,模板继承将不起作用。

于 2012-12-13T06:18:24.690 回答