سؤال

Here is my template code that is causing the error: I cant figure out why. I am using request context in template. I have included django.core.context_processors.request in template processor. Basically I am trying to generate get URL inside the <li></li>

<ul class="list-inline">
          {% if sort_by == "last_updated" %}
         <li> <strong>New Post</strong></li>
         <li><a href="{{ request.get_full_path|add:'?=vehicle_price' }}" style="text-decoration: underline; color: red;">Lowest Price</a></li>
         <li><a href="{{ request.get_full_path|add:'?=vehicle_year' }}" style="text-decoration: underline; color: red;">Latest Model Year</a></li>

          {% elif sort_by == "vehicle_price" %}
          <li><a href="{{ request.get_full_path|add:'?=last_updated' }}" style="text-decoration: underline; color: red;">New Post</a></li>
           <li> <strong>Lowest Price</strong></li>
           <li><a href="{{ request.get_full_path|add:'?=vehicle_year' }}" style="text-decoration: underline; color: red;">Latest Model Year</a></li>

          {% else sort_by == "vehicle_year" %}
           <li><a href="{{ request.get_full_path|add:'?=last_updated' }}" style="text-decoration: underline; color: red;">New Post</a></li>
           <li><a href="{{ request.get_full_path|add:'?=vehicle_price' }}" style="text-decoration: underline; color: red;">Lowest Price</a></li>
           <li> <strong>Latest Model Year</strong></li>
          {% endif %}
      </ul>

This is the error I get: assert token.contents == 'endif'

Traceback:

Traceback:
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  139.                 response = response.render()
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  80.         template = self.resolve_template(self.template_name)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/response.py" in resolve_template
  56.             return loader.select_template(template)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/loader.py" in select_template
  180.             return get_template(template_name)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/loader.py" in get_template
  138.     template, origin = find_template(template_name)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/loader.py" in find_template
  127.             source, display_name = loader(name, dirs)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/loader.py" in __call__
  43.         return self.load_template(template_name, template_dirs)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/loader.py" in load_template
  49.             template = get_template_from_string(source, origin, template_name)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/loader.py" in get_template_from_string
  149.     return Template(source, origin, name)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/base.py" in __init__
  125.         self.nodelist = compile_string(template_string, origin)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/base.py" in compile_string
  153.     return parser.parse()
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/base.py" in parse
  278.                     compiled_result = compile_func(self, token)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/loader_tags.py" in do_extends
  215.     nodelist = parser.parse()
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/base.py" in parse
  278.                     compiled_result = compile_func(self, token)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/loader_tags.py" in do_block
  190.     nodelist = parser.parse(('endblock',))
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/base.py" in parse
  278.                     compiled_result = compile_func(self, token)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/defaulttags.py" in do_if
  942.     nodelist = parser.parse(('elif', 'else', 'endif'))
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/base.py" in parse
  278.                     compiled_result = compile_func(self, token)
File "/Users/sridhar/Documents/virtualenvs/djangocmv/djangocmv/lib/python2.7/site-packages/django/template/defaulttags.py" in do_if
  961.     assert token.contents == 'endif'

Exception Type: AssertionError at /search/139/boston/
Exception Value: 
هل كانت مفيدة؟

المحلول

The syntax of else is not correct:

{% else sort_by == "vehicle_year" %}

Replace it with just {% else %}.

نصائح أخرى

You can't put a condition on an else clause. As in normal Python, if you have a further condition, you need to use elif.

{% if sort_by == "last_updated" %}
   ...
{% elif sort_by == "vehicle_year" %}
   ...
{% else %}
   ...
{% endif %}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top