django custom admin login page

Question:

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?

Asked By: moin moin

||

Answers:

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'
Answered By: John Doe

The 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),
    ...

Docu for the URL dispatcher for reference:

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.

Answered By: SamSparx
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.