2012-12-13 215 views
0

可能重複:
Django templates syntax error嵌套問題?

當我使用{%如果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 %} 

我想一定有問題嵌套如果

+0

當然,有些對齊不會傷害.. – 2012-12-13 06:03:54

+0

你會詳細說明你的評論一點.. –

+1

我認爲他的意思是「你的/ endfor,如果/ endif塊不對齊,並且很難閱讀。 – voithos

回答

2

您不能將{%extends%}標記放在{%if%}之內。它應該是模板中的第一個標記。

從Django文檔Template inheritance

如果你使用模板{%伸出%},它必須是這個模板的第一個模板標籤。模板繼承將不起作用,否則。

+0

但是如果我們把它放在外面,如果條件那麼它會打印出我們不想要的值 –

+0

@ user1896946,在這種情況下,您將不得不重新構建模板,這樣就不必將'{%extends%}'在{%if%}內。 – Rohan