Shane McIntosh
Shane McIntosh

Reputation: 132

Django Invalid block tag: 'else', expected 'endblock'

OK so I'm still fairly new to django and working with it on a personal project. I am receiving this error for the following code:

{% extends "base.html" %}
{% block content %}
    <h1>Website News</h1>
    <% if news_items|length > 0 %>
        {% for post in news_items %}
            <h1>{{post.title}}</h1>
            {% autoescape off %}
                {{post.content}}
            {% endautoescape %}
        {% endfor %}
    {% else %}
        <h1>There are no posts currently</h1>
    {% endif %}
{% endblock %}

Apparently it doesn't like the {% else %} in my if statemtent. I have already done a decent amount of searching and found some somewhat similar issues but their issues all had to do with either improperly nested blocks or a wrong character. I've checked the docs a bunch and can't find anything wrong with my code besides django complaining.

Any ideas?

Upvotes: 1

Views: 7657

Answers (2)

user1409289
user1409289

Reputation: 542

If you are trying to use "if" you have to put that inside {% %} block .

.

Upvotes: 3

Mariusz Jamro
Mariusz Jamro

Reputation: 31673

You have

<% if news_items|length > 0 %>

instead of:

{% if news_items|length > 0 %}

in your code, which is not a correct template tag.

Upvotes: 9

Related Questions