django assertionerror:在模板中断言token.contents =='endif'?

时间:2014-05-10 03:59:11

标签: django django-templates

以下是导致错误的模板代码: 我无法弄清楚为什么。我在模板中使用请求上下文。我在模板处理器中包含了django.core.context_processors.request。基本上我正在尝试在<li></li>

中生成get URL
<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>

这是我得到的错误: assert token.contents == 'endif'

回溯:

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: 

2 个答案:

答案 0 :(得分:2)

else的语法不正确:

{% else sort_by == "vehicle_year" %}

只用{% else %}替换它。

答案 1 :(得分:0)

您不能在else条款中加上条件。与普通Python一样,如果您有其他条件,则需要使用elif

{% if sort_by == "last_updated" %}
   ...
{% elif sort_by == "vehicle_year" %}
   ...
{% else %}
   ...
{% endif %}