2012-07-15 70 views
1

試圖按照Django Inclusion Tag documentation創建自定義模板標記,但在第6行獲取模板語法錯誤:def types(Information)Django內含標記語法錯誤

from django import template 

register = template.Library() 

@register.inclusion_tag('edit.html') 
def types(Information) 
    informations = Information.objects.all() 
    return {'informations': informations} 

templatetag.py文件是/templatetags目錄中。

的信息,型號:

class Information(models.Model): 
    name = models.CharField(max_length=20) 
    models = models.ManyToManyField('Model') 

模板(edit.html):

{% load templatetag %} 
<ul> 
    {% for information in informations %} 
     <li> {{ information }} </li> 
    {% endfor %} 
</ul> 

我誤解了如何創建包含標籤和對象?感謝您的任何建議。

回答

1

好吧,毫不奇怪,你有一個語法錯誤。函數定義,像什麼啓動Python中的塊,必須在最後一個冒號:

def types(information): 

還要注意,由於某種原因,你命名你的論點Information,這將隱藏類信息 - 無論傳遞的對象,因爲實際參數將用作objects.all()查詢的基礎,這不太可能發揮作用。

+0

該死的,再次感謝羅斯曼先生。不能相信我錯過了這一點。 – 2012-07-15 04:51:30