Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the user's permissions in views.py?

Tags:

python

django

In my views.py file on my Django site, I have a class based view that needs to alter a variable based on the user's permissions.

So if a user has the pro_view permission, then it sees one thing. Otherwise if it has basic_view then it sees another.

How can I access this inside my get_context_data(self, **kwargs): function?

like image 251
User Avatar asked Feb 12 '23 15:02

User


1 Answers

Use has_perm:

So, from inside your get_context_data you can do something like this:

if self.request.user.has_perm('applications.admin_access'):
    # do this
else:
    # do that
like image 127
Serafeim Avatar answered Feb 16 '23 10:02

Serafeim