skullkid
skullkid

Reputation: 448

Django template inheritance: repetitive views

I have a view called ListAEQ:

class ListAEQ(MixinView, ListView):
    template_name = 'allometric/aeq_list.html'
    model = Equation

    def get_queryset(self):
        return (Equation.objects.filter(owner=self.request.user))

I want to use the queryset from this view multiple times with different templates. For example I have a template that extends aeq_list.html, that replaces a block in the parent template with different content. How do I render this content using the same view but different templates, without having to create multiple views that have the same queryset and a different tempate_name. I believe there is a way to do this according to the principle "DRY"

For example, I would create a new view

class ListAEQindia(MixinView, ListView):
    template_name = 'allometric/aeq_list_india.html'
    model = Equation

    def get_queryset(self):
        return (Equation.objects.filter(owner=self.request.user))

Upvotes: 0

Views: 176

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599600

You don't say how you're determining which template is to be rendered. But presuming it's based on a parameter from the URL, you can define the get_template_namesmethod in your view.

That method can access self.kwargs and self.request etc and then return a list containing the name of the template to use. (Note that it does have to be a list, even if the list only contains one item.)

Upvotes: 1

Related Questions