Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TemplateSyntaxError Invalid block tag: 'else', expected 'endif'

I'm going through the tango with django tutorial and everything was going well until this problem came up in one of my templates.

It says that it was expecting and endif tag but it found an else tag instead which I don't understand because the inner if/else/endif clause doesn't cause any problems.

I have seen some people having similar issues with custom tags that they did not import at the beginning of their script but these are not custom tags so I don't know what the issue might be. Maybe a syntax error I'm missing?

<body>
    {% if category_name %}
        <h1>{{ category_name }}</h1>
        {% if pages %}
            <ul>
                {% for page in pages %}
                    <li><a href="{{ page.url }}">{{ page.title }}</a></li>
                {% endfor %}
            </ul>
        {% else %}
            <strong>No pages currently in category.</strong>
        {% endif % }
    {% else %}                <= error here: "expected endif instead of else"
        The specified category {{ category_name }} does not exist!
    {% endif %}
</body>

I am using vim with shiftwidth=3, tabstop=3, expandtab. I don't know if that may have to do with this but just in case.

like image 571
Abraham Escalante Avatar asked Feb 14 '26 15:02

Abraham Escalante


1 Answers

You have a little typo: One of your statements ends with {% endif % } instead of {% endif %} (remove a space).

like image 78
mimo Avatar answered Feb 17 '26 08:02

mimo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!