How can I pass two models to a class based generic view

后端 未结 2 591
北恋
北恋 2021-01-28 19:41

I have an app called \"blog\" that has a model called \"Entry\". I use a class based generic to view this Entry and I am happy with this.

Now, along comes another app ca

2条回答
  •  误落风尘
    2021-01-28 20:45

    As it looks to me you would like to have your events on more than one page, a context processor may be the right tool for you. It should allow you to access a queryset of events within the context of every template:

    from eventapp.models import Event
    
    def event_provessor(request):
        return {'events': Event.objects.all()}    
    

    Because of the queryset's laziness you don't have to be afraid to hit the database if you don't need the list, the query is only run if you would iterate over events.

    Of course you could also eg. subclass the views and add the events there to the context, but if more than one view is affected a context processor might make more sense!

提交回复
热议问题