Django Pass Multiple Models to one Template

前端 未结 3 1034
庸人自扰
庸人自扰 2020-11-30 18:20

I am building an address book that includes the relationships between entries, etc. I have separate models for Individuals, Companies, Venues, and Roles. On my index page I

3条回答
  •  天涯浪人
    2020-11-30 18:45

    I suggest you remove your object_list view,

    define a dictionary for this specific view,

       all_models_dict = {
            "template_name": "contacts/index.html",
            "queryset": Individual.objects.all(),
            "extra_context" : {"role_list" : Role.objects.all(),
                               "venue_list": Venue.objects.all(),
                               #and so on for all the desired models...
                               }
        }
    

    and then in your urls:

    #add this import to the top  
    from django.views.generic import list_detail
    
    (r'^$', list_detail.object_list, all_models_dict),
    

提交回复
热议问题