'Django Admin - Preventing staff from accessing admin panel

I'm fairly new to Django.

How do I prevent staff users from logging into the Django admin panel? I still want them to be identified as staff, but do not want them to be able to log into the admin panel. I am using the default Django admin panel.

Thanks



Solution 1:[1]

You can limit admin access to only superusers by overriding the default admin site and overriding the has_permission method in your custom admin site to only return True for superusers

myproject/admin.py

from django.contrib import admin


class MyAdminSite(admin.AdminSite):

    def has_permission(request):
        return request.user.is_active and request.user.is_superuser

myproject/apps.py

from django.contrib.admin.apps import AdminConfig


class MyAdminConfig(AdminConfig):

    default_site = 'myproject.admin.MyAdminSite'

myproject/settings.py

INSTALLED_APPS = [
    ...
    'myproject.apps.MyAdminConfig',  # replaces 'django.contrib.admin'
    ...
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Iain Shelvington