I
I
Igor Lyutoev2015-03-06 12:08:19
Django
Igor Lyutoev, 2015-03-06 12:08:19

How to change path to template in Django view?

Tell me, is it possible to change the path to the template in the view based on some data?
For example, take a simple representation:

class CompanyDetail(DetailView):
    model = Company
    template_name = 'company/detail.html'

From the url we get the 'pk' of the company and do something like this:
temp = Company.objects.get(pk=int(kwargs['pk']))
if temp.template_change:
   template_name = 'company/detail2.html'

So he doesn't want to work. I tried to put it in get_queryset and get_context_data didn't work either.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey K, 2015-03-06
@loader777

class CompanyDetail(DetailView):
    ...
    def get_template_names(self):
        temp = Company.objects.get(pk=int(self.kwargs['pk']))
        if temp.template_change:
            return 'company/detail2.html'
        else:
            return self.template_name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question