Changing the template_name in Templateview (Django)

Asked

Viewed 92 times

1

How do I change template_name in Templateview if user is authenticated?

Look what I 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/'

1 answer

1

Good guy, if you have a different view for an authenticated person and one that isn’t authenticated, study the possibility of leaving a view for each thing. If information handling increases, this will prevent hacking.

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']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.