Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set different 'template_name's inside of django TemplateView?

Using Django views I want to redirect a user based on their permission levels.

I have a template view that works.

class theTableView(generic.TemplateView):
    template_name = 'adminTable.html'

What I am trying to do looks something like this:

class TheTableView(generic.TemplateView):
    if self.request.user.is_superuser==True:
        tempTemplate = 'goodAdminTable.html'
    elseif self.request.user.is_authenticated==True:
        tempTemplate = 'goodUserTable.html'
    template_name = tempTemplate

I was able to do this in the rest_framework api return for the data.

I need to be able to check the user permissions and redirect them to the appropriate template_name based on the results.

Any help is appreciated. Thanks.

like image 618
Jon Kennedy Avatar asked Jan 08 '23 16:01

Jon Kennedy


1 Answers

You can do this by overriding get_template_names() method like this

class theTableView(generic.TemplateView):
    template_name = 'adminTable.html'

    def get_template_names(self):
        if self.request.user.is_superuser:
            template_name = 'goodAdminTable.html'
        elif self.request.user.is_authenticated:
            template_name = 'goodUserTable'
        else:
            template_name = self.template_name
        return [template_name]
like image 121
Nikita Nikitin Avatar answered Jan 15 '23 12:01

Nikita Nikitin