在单个模板中传递DetailView Django多个模型

时间:2018-05-14 18:39:41

标签: django django-templates django-views

如何在单个模板中使用DetailView传递多个模型。

我的观点文件 views.py

from .models import EventDetail, EventTrainer, EventParticipant

class EventDetailView(DetailView):
    # model = EventDetail
    # template_name = 'event_templates/single_event.html'

    def get_context_data(self, *args, **kwargs):
        context = super(EventDetailView, self).get_context_data(*args, **kwargs)
        return context

1 个答案:

答案 0 :(得分:0)

您可以通过context

def get_context_data(self, *args, **kwargs):
    context = super(EventDetailView, self).get_context_data(*args, **kwargs)

    context['event_trainers'] = EventTrainer.objects.all()
    context['event_participants'] = EventParticipants.objects.all()

    return context