成功登录后django管理员重定向 [英] django admin redirect after a successful login

查看:83
本文介绍了成功登录后django管理员重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Django = 1.6.1。



当前在我的项目中,管理系统成功登录后,管理系统将我带到管理仪表板(即:本地主机: 8000 / admin /)。在这里,我可以访问大多数应用程序,例如用户,类别,组等。



我有90%的时间要访问用户管理页面(即localhost: 8000 / admin / my_project / users /)。因此,我试图找到一种方法来将管理员登录名重定向到管理员用户页面而不是仪表板。我怎样才能做到这一点?

解决方案

我不知道这是否是解决此问题的正确方法,但这就是我解决问题的方式。 / p>

我创建了 AdminSite 的子类,称为 CustomAdminSite 并覆盖了 login()以使用REDIRECT_FIELD_NAME(即使用下一个隐藏字段选项)接受重定向URL。

  class CustomAdminSite(AdminSite):
@never_cache
def登录(自己,请求,extra_context =无):

显示给定HttpRequest的登录表单。
django.contrib.auth.views中的
import import
context = {
'title':_('Log in '),
'app_path':request.get_full_path(),
REDIRECT_FIELD_NAME:设置。ADMIN_LOGIN_REDIRECT_URL,
}
context.update(extra_context或{})

默认值= {
'extra_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中。在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管理员重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆