Redirecting to another page after django admin log

2019-07-03 11:06发布

I am making a custom administration page in Django. I do not want to reinvent the wheel and thus want to use Django admin login form for the staff to log in and redirect them to /my-url/ afterwards.

However, I can't find the way to redirect user to a custom url after successful login at /admin/.

4条回答
三岁会撩人
2楼-- · 2019-07-03 11:39

I had the same issue. Instead of redirect after login I used the @staff_member_required decorator for my view /my-url/ which redirects to the admin login

from django.contrib.admin.views.decorators import staff_member_required

@staff_member_required
def test_list(request):
    return HttpResponse('TEST')

If class based views is used check out the method_decorator

查看更多
地球回转人心会变
3楼-- · 2019-07-03 11:40

since I stumbled across the same problem, I noticed the url of the the default login page:

/admin/login/?next=/admin/

so I changed the login page link to

/admin/login/?next=/ 

to point to the main page

works for the logout page too, nice and simple

查看更多
男人必须洒脱
4楼-- · 2019-07-03 11:42

Set LOGIN_REDIRECT_URL in your settings.py file. Documented here.

查看更多
够拽才男人
5楼-- · 2019-07-03 11:54

The Django auth app comes with a login view which you can hook up to /accounts/login/ or any other url you choose. You can probably use the admin's login template admin/login.html if you don't want to write your own.

By using the login view, the LOGIN_REDIRECT_URL parameter will work. The purpose of the /admin/ page is to display the admin index. I would avoid trying to use it as the login page.

查看更多
登录 后发表回答