[Fixed]-How do I set the template_name using generic.TemplateView based on is_authenticated vs is_superuser?

1👍

You need the request so do as you’ve done in MyList (use a function).

class TableView(LoginRequiredMixin, generic.TemplateView):
    def get_template_names(self):
        if self.request.user.is_superuser:
             return 'superadmin/path/template.html'
        return 'regular/path/template.html'

By the way:

if self.request.user.is_superuser==True:

is the same than (better version):

if self.request.user.is_superuser:

And if you use LoginRequiredMixin, you already know that the user is authenticated so your second if statement is useless.

Here is MyList updated:

class MyList(LoginRequiredMixin, generics.ListCreateAPIView):
    serializer_class = MySerializer
    def get_queryset(self):
        if self.request.user.is_superuser:
            return ImportantStuff.objects.all()
        return ImportantStuff.objects.filter(claimed=False)

Leave a comment