如何不使用 Django 的管理员登录视图? [英] How can I not use Django's admin login view?
问题描述
我创建了自己的登录视图.但是,如果用户直接进入/admin,它会将他们带到管理员登录页面并且不使用我的自定义视图.我怎样才能让它重定向到用于所有非/admin 的登录视图?
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?
推荐答案
来自 http://djangosnippets.org/snippets/2127/ — 使用 login_required
包装管理员登录页面.例如,在 urls.py
中:
From http://djangosnippets.org/snippets/2127/—wrap the admin login page with login_required
. For example, in urls.py
:
from django.contrib.auth.decorators import login_required
from django.contrib import admin
admin.autodiscover()
admin.site.login = login_required(admin.site.login)
您可能已经有了中间两行,甚至可能是第一行;添加第四行将导致任何可能命中 admin.site.login
函数的内容都使用适当的 next
参数重定向到您的 LOGIN_URL
.
You probably already have the middle two lines and maybe even the first line; adding that fourth line will cause anything that would have hit the admin.site.login
function to redirect to your LOGIN_URL
with the appropriate next
parameter.
这篇关于如何不使用 Django 的管理员登录视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!