I'm starting to use class based views for an application I'm creating but I'm nots sure how it works.
What I need is to have three different templates, and each template will show different information depending on a model field. My question is if there's a way to have only one class view that can render three different html templates with 3 different contexts, or if I need to create 3 different classes.
Using function based views, I would just do this:
# def humanResourcesView(request): # context = { # 'data' : Document.objects.all().filter(documentType='humanResources'), # } # return render(request, 'main/hr.html', context) # #view to display training documents after click # def trainingView(request): # context = { # 'data' : Document.objects.all().filter(documentType='training'), # } # return render(request, 'main/training.html', context) # #view to display resource documents after click # def reportsView(request): # context = { # 'data' : Document.objects.all().filter(documentType='reports') # } # return render(request, 'main/reports.html', context) But I'm not sure how it works with class based views. Currently I have this, which renders and filters data correctly for one template, but I don't know how to do multiple templates. Do I need to create 3 different classes?
class DocumentView(View): def get(self, request, *args, **kwargs): reports = Document.objects.all().filter(documentType="reports") context = { 'reports' : Document.objects.all().filter(documentType='reports') } return render(request, 'documents/reportsDocs.html', context) Is there a way to only have one class, and pass a certain context depen
https://stackoverflow.com/questions/66130042/django-rendering-different-templates-in-one-class-based-view February 10, 2021 at 10:19AM
没有评论:
发表评论