Make a dynamic sidebar based on permissions of users Django

Viewed 488

I have multiple users with multiple permission (admin and user). example: admin is able to see sidebar a,b but user can only see sidebar c.

How i can make it With Django ?

Thanks

1 Answers

If you want to apply permissions by users to specific operations such as List, Add, Delete ect ... you can use this, logically users must previously have their permissions assigned, this can be done from the administration panel, if users already have the permissions assigning, just use them, from the view or from the template, like this:

views.py

@method_decorator(permission_required('model_name.add_model_name', reverse_lazy('forbbiden')), name="dispatch")
class Add_Model_Name(CreateView):
    model = Model_Name
    .
    .
    .

template:

{% if perms.add_model_name %}
    <sidebar a>
    <sidebar b>
{% endif %}

{% if not perms.add_model_name %}
    <sidebar c>
{% endif %}

where add_model_name refers to the name of the permission you want to check, Once the user has logged in, you can directly access the perms variable in the template, asking for specific permissions, example:

{% if perms.add_model_name %} # if have permission to add a model
{% if perms.delete_model_name %} # if havepermission to delete a model
{% if perms.your_own_permission %} #if have a specific permission created by you

what would be something like:

{% if perms.is_admin %}
    <sidebar a>
    <sidebar b>
{% endif %}

{%if perms.is_user %}
    <sidebar c>
{% endif %}
Related