0

2 つの別個の Django モデルを表示する 1 つのページを作成したいと思います。

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 に答える