Redirecting to another page after django admin login

15,090

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

Share:
15,090
aemdy
Author by

aemdy

Updated on June 12, 2022

Comments

  • aemdy
    aemdy over 1 year

    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/.

  • mlwn
    mlwn over 7 years
    @Daniel Backman , I know this is old, but how would you use this method when you are using class-based views, please ??
  • Daniel Backman
    Daniel Backman about 7 years
  • mlwn
    mlwn about 7 years
    @DanielBackman I solved the issue by using mixin... but your solution looks neater. Unfortunately, login page is redirecting me to localhost/accounts/login ... oops.. 404. I have already made changes to the admin template by using myadmin, copying the template to my project folder ... so, login should direct to localhost/myadmin/login instead.. any ideas please ?
  • mlwn
    mlwn about 7 years
    am sorry, I mean I didn't copy the template to my project, but by adding my own admin.py to main app in project and redirecting my homepage to it... I hope I explained well...
  • mlwn
    mlwn about 7 years
    Sorry.. :) you're great ... solved... I only have to specify the arguments of login_required as login_required(redirect_field_name='next', login_url='the login url') .... lol ... thanks alot....
  • dark_prince
    dark_prince about 2 years
    how to do this?