简体   繁体   中英

Django Inclusion Tag SyntaxError

Trying to follow the Django Inclusion Tag documentation to create a custom template tag, but getting a template syntax error on line 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}

The templatetag.py file is within the /templatetags directory.

The model for Information:

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

The template (edit.html):

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

Am I misunderstanding how to create the inclusion tag and objects? Thanks for any advice.

Well, not surprisingly, you have a syntax error. Function definitions, like anything that starts a block in Python, need to have a colon at the end:

def types(information):

Also note that for some reason you've named your argument Information , which will hide the class Information - whatever object you pass as the actual parameter will be used as the base for the objects.all() query, which is unlikely to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM