Home > Blockchain >  Replace Django Admin Login page
Replace Django Admin Login page

Time:12-09

I need to replace the Django Admin Login page. The reason for that is that I've added some extra authentication at my own login page, however, I don't know how to override the login on the admin site.

CodePudding user response:

Here is the solution. Inside urls.py, add the path to the new login page above your admin URLs like this

path('admin/login/', login_view, name='new_admin_login'),  # login_view is the custom login view
path('admin/', admin.site.urls),

CodePudding user response:

Creating a custom AdminSite is the Django way of doing such things. Specifically, in your case set the AdminSite.login_form

from django.contrib.admin import AdminSite
from django.contrib.auth.forms import AuthenticationForm
from django.urls import path


class CustomAuthenticationForm(AuthenticationForm):
    # override the methods you want
    ...


class CustomAdminSite(AdminSite):
    login_form = CustomAuthenticationForm


admin_site = CustomAdminSite()

urlpatterns = [
    path("admin/", admin_site.urls),
]
  • Related