2016-11-09 47 views
1

所以,我通过使用Tango Django这本书的教程中途尝试拼命地尽力获取尽可能多的关于Django的信息。在{%load rango_template_tags%}上打破模板

现在我想建立一个模板,列出所有类别的,但我得到一个错误

invalid syntax (rango_template_tags.py, line 8) 

我不知道为什么我收到这条线,绝对没有我与书检查它5 +次,但我无法找到任何看起来不对的地方。任何人都可以告诉我为什么我得到这个错误。

base.html文件

{% load rango_template_tags %} 
<div> 
    {% block sidebar_block %} 
     {% get_category_list %} 
    {% endblock %} 
</div> 
# This file has more within it these are the new pieces of code that break the template system. If these are in it wont work. 

rango_template_tags

from django import template 
from rango.models import Category 

register = template.Library() 

@register.inclusion_tag('rango/cats.html') 
def get_category_list(): 
return {'cats' Category.objects.all()} 

cats.html

<ul> 
    {% if cats %} 
    {% for c in cats %} 
     <li><a href="{% url 'show_category' c.slug %}">{{ c.name }}</a></li> 
    {% endfor %} 
    {% else %} 
     <li><strong> There ar eno categories presen. </strong></li> 
    {% endif %} 
</ul> 
+1

该行至少有两个错误。再检查一遍。 –

+0

@DanielRoseman你可以进一步细谈吗? –

+0

你用书5 +次检查过,我不知道那本书是哪本书?缩进'return'行并在字典键(''cats'')后面加上冒号。 – Selcuk

回答

1

在Python字典,每个按键从它的值由冒号(:)分隔

从{ '猫' Category.objects.all()}更改return语句{ '猫':类别。 object.all()}和函数或块中的代码应该被设为

from django import template 
from rango.models import Category 

register = template.Library() 

@register.inclusion_tag('rango/cats.html') 
def get_category_list(): 
    return {'cats': Category.objects.all()} 
+0

请提供一些解释以提高答案的质量。请参阅此处以获取[写作答案](http://stackoverflow.com/help/how-to-answer)上的帮助。 – jacefarm