2014-05-10 78 views
1

這是我的模板代碼,導致錯誤: 我無法弄清楚爲什麼。我在模板中使用請求上下文。我在模板處理器中包含了django.core.context_processors.request。基本上我試圖生成內部<li></li>django assertionerror:在模板中聲明token.contents =='endif'?

<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> 

這GET URL是錯誤,我得到: 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: 
+0

您能否顯示完整的回溯? – alecxe

+0

@alecxe:完整回溯粘貼上面 – eagertoLearn

回答

2

else語法不正確:

{% else sort_by == "vehicle_year" %} 

{% else %}替換它。

0

您不能在條款else條款。和普通的Python一樣,如果你有更多的條件,你需要使用elif

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