User
User

Reputation: 24749

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

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?

Upvotes: 6

Views: 3010

Answers (1)

Serafeim
Serafeim

Reputation: 15104

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

Upvotes: 10

Related Questions