2011-05-09 31 views
1

这应该是非常简单的,它在文档中。我正在尝试获取ListView的上下文。例如:在外键中获取父项的上下文 - Django

“我们还可以添加的发布商在同一时间范围内,因此我们可以在模板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 

在标题内,我的问题清单,我需要返回标题和标题的所有其他属性。说起来还有不工作,则返回错误:

get_context_data() got an unexpected keyword argument 'object_list'

回答

2

您应该使用** kwargs在get_context_data

def get_context_data(self, **kwargs): 
    context = super(IssuesByTitleView, self).get_context_data(**kwargs) 
    context['title'] = self.title 
    return context 
+0

应怎样使用** kwargs,programmersbook? – AAA 2011-05-09 08:27:04

+0

没关系,谢谢! – AAA 2011-05-09 08:31:28

+0

它只是将给定的键值参数传递给下一个方法 – programmersbook 2011-05-09 09:03:01