【问题标题】:Django Inclusion Tag SyntaxErrorDjango 包含标记语法错误
【发布时间】:2012-07-15 04:47:38
【问题描述】:

尝试按照 Django Inclusion Tag documentation 创建自定义模板标签,但在第 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>

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

【问题讨论】:

    标签: python django templates object tags


    【解决方案1】:

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

    def types(information):
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 2013-01-04
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多