Answers for "django user permissions"

4

loginrequiredmixin

from django.contrib.auth.mixins import LoginRequiredMixin

LOGIN_URL = 'your_url'
Posted by: Guest on October-18-2020
1

Django: checking permission on user

from django.contrib.auth.decorators import permission_required

@permission_required('auth.view_user')
def users_list_view(request):
    pass
Posted by: Guest on June-07-2021
1

django user permission check

# all your import goes here for Models.py
...

# Adding Permissions to a Model.
class BlogPost(models.Model):
  ... # model fields
  class Meta:
    permissions = [('can_write_blog', 'Can Write Blog')]
    

# Checking permissions in Views.py
if request.user.has_perm('app_name.can_write_blog'):
  # give access to blog post form
else:
  # restrict user from access the page.
Posted by: Guest on October-13-2020
0

Django: checking permission on user

from typing import Set

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

@admin.register(User)
class CustomUserAdmin(UserAdmin):
    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj, **kwargs)
        is_superuser = request.user.is_superuser
        disabled_fields = set()  # type: Set[str]

        if not is_superuser:
            disabled_fields |= {
                'username',
                'is_superuser',
                'user_permissions',
            }

        for f in disabled_fields:
            if f in form.base_fields:
                form.base_fields[f].disabled = True

        return form
Posted by: Guest on June-07-2021
0

Django: checking permission on user

{% if perms.auth.delete_user %}
<button>Delete user!</button>
{% endif %}
Posted by: Guest on June-07-2021

Code answers related to "django user permissions"

Python Answers by Framework

Browse Popular Code Answers by Language