django admin成功登录后重定向 [英] django admin redirect after a succesful login
问题描述
我正在使用Django = 1.6.1。
目前在我的项目中,管理系统在管理员登录成功后,请访问管理控制台(即:localhost: 8000 /管理/)。从这里我可以访问我的大部分应用程序,如用户,类别,组等。
90%的时间我想访问用户管理页面(即localhost: 8000 /管理/ MY_PROJECT /用户/)。因此,我试图找到一种方法来将管理员登录重定向到管理员用户页面而不是仪表板。我该怎么做?
我不知道这是否是解决这个问题的正确方法,但这是我如何解决我的问题。 / p>
我创建了一个 AdminSite
的子类称为 CustomAdminSite
并覆盖 login()
使用REDIRECT_FIELD_NAME接受重定向网址(即使用下一个隐藏字段选项)。
class CustomAdminSite(AdminSite):
@never_cache
def login(self,request,extra_context = None):
显示给定HttpRequest的登录表单
from django.contrib.auth.views import login
context = {
'title':_('登录'),
'app_path':request.get_full_path(),
REDIRECT_FIELD_NAME:settings.ADMIN_LOGIN_REDIRECT_URL,
}
context.update(extra_context或{})
defaults = {
'extra_context':context,
'current_app':self.name,
'authentication_form':self.login_form或AdminAuthenticationForm,
'template_name':self.login_template或'admin / login.html ',
}
返回登录(请求,**默认值)
site = CustomAdminSite()
在urls.py
admin.site.login = path.to. custom.site.login
在settings.py
ADMIN_LOGIN_REDIRECT_URL ='/ admin / my_project / users /'
PS:如果有更好的方法,请让我知道:)
I am using Django=1.6.1.
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% 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.
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()
in urls.py
admin.site.login = path.to.custom.site.login
in settings.py
ADMIN_LOGIN_REDIRECT_URL = '/admin/my_project/users/'
P.S: please let me know if there is a better way to do this :)
这篇关于django admin成功登录后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!