How can I not use Django's admin login view?

Viewed 22452

I created my own view for login. However if a user goes directly to /admin it brings them to the admin login page and doesn't use my custom view. How can I make it redirect to the login view used for everything not /admin?

10 Answers

You can redirect admin login url to the auth login view :

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('your_app.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('admin/login/', RedirectView.as_view(url='/accounts/login/?next=/admin/', permanent=True)),
    path('admin/', admin.site.urls),
]

As of August 2020, django.contrib.admin.sites.AdminSite has a login_template attribute. So you can just subclass AdminSite and specify a custom template i.e.,

class MyAdminSite(AdminSite):
    login_template = 'my_login_template.html'

my_admin_site = MyAdminSite()

Then just use my_admin_site everywhere instead of admin.site.

Related