In your main urls.py file, comment out or remove the admin URL pattern:
python
from django.urls import path, include
urlpatterns = [
# path('admin/', admin.site.urls), # Comment this out
path('', include('your_app.urls')),
# other URL patterns
]
Method 2: Remove Admin from INSTALLED_APPS
In your settings.py, remove or comment out the admin-related apps:
python
INSTALLED_APPS = [
# 'django.contrib.admin', # Remove this
# 'django.contrib.contenttypes', # Remove if not needed elsewhere
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# your apps
]
Method 3: Conditional Admin Access
If you want to disable admin conditionally (e.g., in production), you can use settings:
python
# settings.py
ADMIN_ENABLED = False # Set to True when you need admin access
# urls.py
from django.conf import settings
from django.urls import path, include
urlpatterns = [
path('', include('your_app.urls')),
]
if settings.ADMIN_ENABLED:
from django.contrib import admin
urlpatterns += [
path('admin/', admin.site.urls),
]
Method 4: Use Middleware to Block Access
Create custom middleware to return 404 for admin URLs:
python
# middleware.py
from django.http import Http404
class DisableAdminMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if request.path.startswith('/admin/'):
raise Http404("Admin is disabled")
return self.get_response(request)
Then add it to MIDDLEWARE in settings.py.
Method 1 is usually the simplest and most effective approach for permanently disabling admin access.
User
I want to keep the admin site, but I have a different authentication method with oauth. I want to remove the page that prompts for login and pasword
Assistant
You can customize the Django admin to use OAuth authentication instead of the default login form. Here are the best approaches:
Method 1: Override Admin Login View
Create a custom admin site that redirects to your OAuth login:
python
# admin.py or a separate file like custom_admin.py
from django.contrib.admin import AdminSite
from django.shortcuts import redirect
from django.urls import reverse
class CustomAdminSite(AdminSite):
def login(self, request, extra_context=None):
# Redirect to your OAuth login instead of showing login form
if not request.user.is_authenticated:
return redirect('your_oauth_login_url') # Replace with your OAuth URL
return super().login(request, extra_context)
# Create instance of custom admin
admin_site = CustomAdminSite(name='custom_admin')
# Register your models with the custom admin
from django.contrib.auth.models import User, Group
admin_site.register(User)
admin_site.register(Group)
# Register other models as needed
Then update your urls.py:
python
from django.urls import path
from .admin import admin_site # Import your custom admin
urlpatterns = [
path('admin/', admin_site.urls),
# your other URLs
]
Method 2: Override Login Template
Create a custom login template that immediately redirects:
# middleware.py
from django.shortcuts import redirect
from django.urls import reverse
class AdminOAuthMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if (request.path == '/admin/login/' and
not request.user.is_authenticated):
return redirect('your_oauth_login_url')
return self.get_response(request)
Add to MIDDLEWARE in settings.py:
python
MIDDLEWARE = [
'your_app.middleware.AdminOAuthMiddleware',
# other middleware
]
Method 4: URL Override
Override the admin login URL directly:
python
# urls.py
from django.contrib import admin
from django.urls import path
from django.shortcuts import redirect
def oauth_redirect(request):
return redirect('your_oauth_login_url')
urlpatterns = [
path('admin/login/', oauth_redirect, name='admin_login_redirect'),
path('admin/', admin.site.urls),
# other URLs
]
Method 1 (Custom Admin Site) is usually the cleanest approach as it gives you full control over the admin authentication flow while keeping all admin functionality intact.
Make sure your OAuth callback properly sets request.user and handles admin permissions (is_staff, is_superuser) for users who should access the admin interface.