Exchanging template_name in TemplateView (Django)

1

How do I change the template_name in TemplateView if the user is authenticated?

Look what I've tried

class Home(TemplateView):
    # template_name = 'index.html'

    is_auth = False

    def get(self, request):
        if not request.user.is_authenticated:
            self.is_auth = True
            return HttpResponse('Não')

    def get_template_names(self):
        if self.is_auth:
            return ['%s.html' % self.kwargs['template']]
        # else:
            # return '/crm/employee/add/'
    
asked by anonymous 03.04.2016 / 07:03

1 answer

1

Good face, if you have a different view for an authenticated person and one that is not authenticated, study the possibility of leaving a view for each thing. If the processing of information increases, this will prevent gambiarras.

But answering your question:

class Home(TemplateView):
    def get_template_names(self):
        if self.request.user.is_authenticated():
            return ['template_logado.html']
        else:
            return ['template_publico.html']
    
07.04.2016 / 15:36