django admin 成功登录后重定向 [英] django admin redirect after a successful login
问题描述
我正在使用 Django=1.6.1.
I am using Django=1.6.1.
目前在我的项目中,在管理员成功登录后,管理系统将我带到管理仪表板(即:localhost:8000/admin/).从这里我可以访问我的大多数应用程序,例如用户、类别、组等.
Currently in my project, the admin system takes me to admin dashboard after a successful admin login (i.e: localhost:8000/admin/). From here I can visit most of my apps like users, categories, groups etc..
90% 的时间我想访问用户管理页面(即 localhost:8000/admin/my_project/users/).因此,我试图找到一种将管理员登录重定向到管理员用户页面而不是仪表板的方法.我怎样才能做到这一点?
90% of the time I want to visit the users admin page (i.e localhost:8000/admin/my_project/users/). thus I am trying to find a way to redirect the admin login to admin users page rather than the dashboard. How can I do this?
推荐答案
我不知道这是否是解决这个问题的正确方法,但这就是我解决问题的方法.
I do not know if this is the right way to tackle this issue but this is how I have solved my problem.
我创建了一个名为 CustomAdminSite
的 AdminSite
子类并覆盖了 login()
以接受使用 REDIRECT_FIELD_NAME 的重定向 url(即使用'next' 隐藏字段选项).
I created a subclass of AdminSite
called CustomAdminSite
and overwritten the login()
to accept a redirect url using the REDIRECT_FIELD_NAME(i.e using the 'next' hidden field option).
class CustomAdminSite(AdminSite):
@never_cache
def login(self, request, extra_context=None):
"""
Displays the login form for the given HttpRequest.
"""
from django.contrib.auth.views import login
context = {
'title': _('Log in'),
'app_path': request.get_full_path(),
REDIRECT_FIELD_NAME: settings.ADMIN_LOGIN_REDIRECT_URL,
}
context.update(extra_context or {})
defaults = {
'extra_context': context,
'current_app': self.name,
'authentication_form': self.login_form or AdminAuthenticationForm,
'template_name': self.login_template or 'admin/login.html',
}
return login(request, **defaults)
site = CustomAdminSite()
在 urls.py 中
in urls.py
admin.site.login = path.to.custom.site.login
在 settings.py 中
in settings.py
ADMIN_LOGIN_REDIRECT_URL = '/admin/my_project/users/'
P.S:如果有更好的方法,请告诉我:)
P.S: please let me know if there is a better way to do this :)
这篇关于django admin 成功登录后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!