Disallowing access to Django admin for non-staff users
Websites built with Django often have their own login page, making the admin login page unnecessary. Django does not provide a setting to disable the admin login page while keeping the rest of the dashboard.
A simple middleware can prevent anonymous and regular users from seeing the admin dashboard:
from django.http import Http404
from django.urls import reverse
def hide_django_admin_middleware(get_response):
def middleware(request):
if request.path.startswith(reverse('admin:index')):
if not request.user.is_staff:
raise Http404()
return get_response(request)
return middleware