Setting an option that can only be viewed by users in a certain group

I am quite new to django and am building a project using django and python predominantly.

I have two user groups in djangos admin panel created and defined there with user added through this admin panel; Diving_Officers and Club_Members

In my webpage, i have an option that i want to only be visible or even clickable to the users in one group, the Diving_Officers group. I cannot find any specific information for how to call a group that exists in django admin and assign it permissions or how to limit a view to it.

**in short how do i add a set of permissions to a group, ie. people in this group can only view this page

On a view you can use the decorator @user_passes_test() (you can find more informations here

to make the option visible only to user in that group instead you can use a template filter, create a "templatetags" folder in your app and add this into "" in that folder:

from django import template

register = template.Library()

def has_group(user, group_name):
     return user.groups.filter(name=group_name).exists()

then, after loading in the template with "load has_group" you can use user|has_group:"mygroup" as if condition

Back to Top