Home > Software design >  django custom admin login page
django custom admin login page

Time:11-11

I have a custom auth mechanism for users and I want to use the same for django admin. All works fine for authenticated users but if an unauthenticated user opens the url /admin he is redirected to /admin/login with the std. login page for admin. I want to redirect to auth/sign-in or block the page.

urlpatterns = [
    path('admin/', admin.site.urls),
    path('admin/login', SignInView.as_view(), name='admin/login'),
    ...

Redefining the url like in the code block does not work. Any idea?

CodePudding user response:

Define these in your settings.py file.

  • LOGIN_REDIRECT_URL
  • LOGIN_URL
  • LOGOUT_REDIRECT_URL

Example:

LOGIN_REDIRECT_URL = '/accounts/dashboard'

LOGIN_URL = '/accounts/signin'

LOGOUT_REDIRECT_URL = '/accounts/signin'

CodePudding user response:

I think urlpatterns are searched in order, using the first match, rather than overwitten by later patterns, so you might have better luck with:

urlpatterns = [
    path('admin/login', SignInView.as_view(), name='admin/login'),
    path('admin/', admin.site.urls),
    ...

(I'd probably also avoid a slash in the name="" just to avoid confusion)

  • Related