【发布时间】:2011-05-09 08:06:12
【问题描述】:
这应该很简单,并且在文档中。我正在尝试获取 ListView 的上下文。例如:
"我们也可以同时将发布者添加到上下文中,这样我们就可以在模板http://docs.djangoproject.com/en/1.3/topics/class-based-views/#dynamic-filtering/中使用它:
class IssuesByTitleView(ListView):
context_object_name = "issue_list"
def get_queryset(self):
self.title = get_object_or_404(Title, slug=self.kwargs['title_slug'])
return Issue.objects.filter(title=self.title).order_by('-number')
我的 models.py 看起来像这样:
class Title(models.Model):
CATEGORY_CHOICES = (
('Ongoing', 'Ongoing'),
('Ongoing - Canceled', 'Ongoing - Canceled'),
('Limited Series', 'Limited Series'),
('One-shot', 'One-shot'),
('Other', 'Other'),
)
title = models.CharField(max_length=64)
vol = models.IntegerField(blank=True, null=True, max_length=3)
year = models.CharField(blank=True, null=True, max_length=20, help_text="Ex) 1980 - present, 1980 - 1989.")
category = models.CharField(max_length=30, choices=CATEGORY_CHOICES)
is_current = models.BooleanField(help_text="Check if the title is being published where Emma makes regular appearances.")
slug = models.SlugField()
class Meta:
ordering = ['title']
def get_absolute_url(self):
return "/titles/%s" % self.slug
def __unicode__(self):
return self.title
class Issue(models.Model):
CATEGORY_CHOICES = (
('Major', 'Major'),
('Minor', 'Minor'),
('Cameo', 'Cameo'),
('Other', 'Other'),
)
title = models.ForeignKey(Title)
number = models.IntegerField(help_text="Do not include the '#'.")
.........
Views.py:
class IssuesByTitleView(ListView):
context_object_name = "issue_list"
def get_queryset(self):
self.title = get_object_or_404(Title, slug=self.kwargs['title_slug'])
return Issue.objects.filter(title=self.title).order_by('-number')
def get_context_data(self):
context = super(IssuesByTitleView, self).get_context_data()
context['title'] = self.title
return context
在我的 Title 问题列表中,我需要返回 Title 和 Title 的所有其他属性。上面的那个不起作用,它返回一个错误:
get_context_data() 得到了一个意外的关键字参数“object_list”
【问题讨论】:
标签: django django-views