按照Practical Django教程获取此错误。在这里和谷歌搜索过,并没有找到任何提及这一点,所以我想这将是我想念的一件容易事。例外是gambino_tags.py:
context[self.varname] = self.model._default_manager.all()[:self.num]
models.py
class Entry(models.Model):
# Entry types
LIVE_STATUS = 1
DRAFT_STATUS = 2
HIDDEN_STATUS = 3
STATUS_CHOICES = (
(LIVE_STATUS, 'Live'),
(DRAFT_STATUS, 'Draft'),
(HIDDEN_STATUS, 'Hidden'),
)
# Foreign Key
author = models.ForeignKey(User)
title = models.CharField(max_length=250, help_text='Maximum 250 characters.')
# Bit of redundancy with excerpt & excerpt_html and body & body_html, but seems to be cleanest way to seperate plain text and html
excerpt = models.TextField(blank=True, help_text='Add an excerpt - a short summary of your post.')
excerpt_html = models.TextField(editable=False, blank=True)
body = models.TextField(help_text='Add the content of your post.')
body_html = models.TextField(editable=False, blank=True)
pub_date = models.DateTimeField(default=datetime.datetime.now)
# Slug prepopulated from title in admin.py
slug = models.SlugField(unique_for_date='pub_date', help_text="Suggested value automatically generated from title. Must be unique.")
enable_comments = models.BooleanField(default=True)
featured = models.BooleanField(default=False, help_text="Make this post featured. Will provide additional sorting.")
status = models.IntegerField(choices=STATUS_CHOICES, default=LIVE_STATUS)
categories = models.ManyToManyField(Category)
tags = TagField()
# Adds objects and live override from LiveEntryManager class above
live = LiveEntryManager()
objects = models.Manager()
gambino_tags.py
from django import template
from django.db.models import get_model
def do_latest_content(parser, token):
bits = token.contents.split()
# Checks to make sure that there are 5 words in the tag
if len(bits) != 5:
raise template.TemplateSyntaxError("'get_latest_content' tag takes exactly four arguments")
model_args = bits[1].split('.')
# Checks to make sure first argument is appname.model name
if len(model_args) != 2:
raise template.TemplateSyntaxError("First argument to 'get_latest_content' must be an 'application name'.'model name' string")
model = get_model(*model_args)
# Checks to make sure model is != none
if model is None:
raise template.TemplateSyntaxError("'get_latest_content' tag got an invalid model: %s" % bits[1])
return LatestContentNode(bits[1], bits[2], bits[4])
class LatestContentNode(template.Node):
def __init__(self, model, num, varname):
self.model = model
# convert num to int
self.num = int(num)
self.varname = varname
def render(self, context):
# uses default manager for Entry.live.all() instances
context[self.varname] = self.model._default_manager.all()[:self.num]
return ''
register = template.Library()
register.tag('get_latest_content', do_latest_content)
base.html文件
{% load gambino_tags %}
<h2>Five latest entries:</h2>
<ul>
{% get_latest_content gambino.entry 5 as latest_entries %}
{% for entry in latest_entries %}
<li>
<a class="list" href="{{ entry.get_absolute_url }}">{{ entry.title }}</a>
Posted {{ entry.pub_date|timesince }} ago.
</li>
{% endfor %}
</ul>
<h2>Five latest links:</h2>
<ul>
{% get_latest_content gambino.link 5 as latest_links %}
{% for link in latest_links %}
<li>
<a href="{{ link.get_absolute_url }}">{{ link.title }}</a>
Posted {{ link.pub_date|timesince }} ago.
</li>
{% endfor %}
</ul>
谢谢!
答案 0 :(得分:1)
model
Node
的{{1}}参数是一个字符串(或者至少是你传入的字符串),而不是__init__
个对象。
Model
也许你的意思是......
bits = token.contents.split()
...
return LatestContentNode(bits[1], bits[2], bits[4])
...
class LatestContentNode(template.Node):
def __init__(self, model, num, varname):
self.model = model
...
context[self.varname] = self.model._default_manager.all()[:self.num]