TemplateSyntaxError Unclosed tag on line 180: 'if'. Looking for one of: elif, else, endif.

Raised by:

django/template/base.py (line 516)

Traceback (most recent call last):
  File "python2.7/site-packages/django/core/handlers/base.py", line 174, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "python2.7/site-packages/django/core/handlers/base.py", line 172, in get_response
    response = response.render()
  File "python2.7/site-packages/django/template/response.py", line 160, in render
    self.content = self.rendered_content
  File "python2.7/site-packages/django/template/response.py", line 137, in rendered_content
    content = template.render(context, self._request)
  File "python2.7/site-packages/django/template/backends/django.py", line 95, in render
    return self.template.render(context)
  File "python2.7/site-packages/django/template/base.py", line 206, in render
    return self._render(context)
  File "python2.7/site-packages/django/test/utils.py", line 92, in instrumented_test_render
    return self.nodelist.render(context)
  File "python2.7/site-packages/django/template/base.py", line 992, in render
    bit = node.render_annotated(context)
  File "python2.7/site-packages/django/template/base.py", line 959, in render_annotated
    return self.render(context)
  File "python2.7/site-packages/django/template/defaulttags.py", line 326, in render
    return nodelist.render(context)
  File "python2.7/site-packages/django/template/base.py", line 992, in render
    bit = node.render_annotated(context)
  File "python2.7/site-packages/django/template/base.py", line 959, in render_annotated
    return self.render(context)
  File "python2.7/site-packages/django/template/loader_tags.py", line 209, in render
    return template.render(context)
  File "python2.7/site-packages/django/template/base.py", line 208, in render
    return self._render(context)
  File "python2.7/site-packages/django/test/utils.py", line 92, in instrumented_test_render
    return self.nodelist.render(context)
  File "python2.7/site-packages/django/template/base.py", line 992, in render
    bit = node.render_annotated(context)
  File "python2.7/site-packages/django/template/base.py", line 959, in render_annotated
    return self.render(context)
  File "python2.7/site-packages/django/template/defaulttags.py", line 584, in render
    return self.nodelist.render(context)
  File "python2.7/site-packages/django/template/base.py", line 992, in render
    bit = node.render_annotated(context)
  File "python2.7/site-packages/django/template/base.py", line 959, in render_annotated
    return self.render(context)
  File "python2.7/site-packages/django/template/loader_tags.py", line 200, in render
    template = context.template.engine.get_template(template_name)
  File "python2.7/site-packages/django/template/engine.py", line 190, in get_template
    template, origin = self.find_template(template_name, dirs)
  File "python2.7/site-packages/django/template/engine.py", line 157, in find_template
    name, template_dirs=dirs, skip=skip,
  File "python2.7/site-packages/django/template/loaders/base.py", line 46, in get_template
    contents, origin, origin.template_name, self.engine,
  File "python2.7/site-packages/django/template/base.py", line 189, in __init__
    self.nodelist = self.compile_nodelist()
  File "python2.7/site-packages/django/template/base.py", line 231, in compile_nodelist
    return parser.parse()
  File "python2.7/site-packages/django/template/base.py", line 516, in parse
    raise self.error(token, e)
TemplateSyntaxError: Unclosed tag on line 180: 'if'. Looking for one of: elif, else, endif.

How to reproduce

{% if var1 %} foo {% else %} {% if var2 %} bar

How to fix

The following is incorrect:

{% if var1 %}
    foo
{% else %}
{% if var2 %}
    bar
{% endif %}

All ifs must be ended with endif. Thus the correct is:

{% if var1 %}
    foo
{% elif var2 %}
    bar
{% endif %}

Or (depends on desired behavior):

{% if var1 %}
    foo
{% endif %}
{% if var2 %}
    bar
{% endif %}