1

尝试按照Django 包含标记文档创建自定义模板标记,但在第 6 行收到模板语法错误:def types(Information).

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>

我是否误解了如何创建包含标签和对象?感谢您的任何建议。

4

1 回答 1

1

好吧,毫不奇怪,您有语法错误。函数定义,就像任何在 Python 中开始一个块的东西一样,需要在末尾有一个冒号:

def types(information):

另请注意,由于某种原因,您已命名您的参数Information,这将隐藏类信息 - 您作为实际参数传递的任何对象都将用作objects.all()查询的基础,这不太可能起作用。

于 2012-07-15T04:50:01.493 回答