0

我想创建一个显示两个独立 Django 模型的页面:

class Client(models.Model):
    name = models.CharField(max_length=100)
    slug = AutoSlugField(populate_from='name', blank=True, unique=True)
    order = models.IntegerField(editable=False, default=0)

    class Meta:
        ordering = ('order',)
    def __unicode__(self):
        return self.name

class Press(models.Model):
    title = models.CharField(max_length=50)
    article = models.ImageField(upload_to = 'images')

    def image_thumb(self):
        if self.article:
            return u'<img src="%s" height="125"/>' %self.article.url
        else:
            return "no image"

    image_thumb.short_description = "article"
    image_thumb.allow_tags = True

    class Meta:
        verbose_name_plural = "press"

我不确定如何在 Views.py 中编写我的查询集。我试过这样的东西......

class ClientView(generic.ListView):    
    template_name = 'clients.html'
    context_object_name = 'client'

    def queryset(request):
        client_page = {'press': Press.objects.all(), 'client': Clients.objects.all()}
        return client_page

然后在我的 urls.py 中...

url(r'^clients/', views.ClientView.as_view(), name = 'client_model'),

我在堆栈答案中读到,我可以通过使用“get_extra_context”来做到这一点,但有人可以告诉我它是如何使用的吗?

4

1 回答 1

2
class ClientView(generic.ListView):
    # ...

    def get_context_data(self, **kwargs):
        context = super(ClientView, self).get_context_data(**kwargs)
        context['press'] = Press.objects.all()
        return context
于 2013-08-25T20:41:40.783 回答