0

Possible Duplicate:
Django templates syntax error

When i am using {% if request.user.is_authenticated %} condition for redirection on this code it throws error 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 i am using this script then it gives no error

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

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

I think there must be a problem with nested if .

Community
  • 1
  • 1

1 Answers1

2

You can't put {%extends%} tag inside {%if%}. It should be first tag in the template.

From django docs Template inheritance

If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won't work, otherwise.

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • but if we put it outside if condition then it prints the value which we doesnot want – Rahul ghrix Dec 13 '12 at 06:21
  • @user1896946, in that case you will have to re-structure for templates so that you don't have to put `{%extends%}` within `{%if%}`. – Rohan Dec 13 '12 at 07:12