Wednesday, October 19, 2022

[FIXED] how to redirect my own login to admin login in django

Issue

I created my own login template(the one suggested by Django documentation) which forwards to a view called hola (the main page), this one uses hola.html template. What I pretend is to forward admin users to the admin application's main page and non-admin users to hola.py/hola.html.

this is my hola view

def hola(request):
    if request.user.is_admin:
        return HttpResponseRedirect('/admin/')
    else:
        return render(request,'hola.html',{'usuario':request.user})

Solution

In your hola view, check if the user is an admin (you can use User.is_staff or User.is_superuser). If so, redirect them to the admin main page:

from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

...
return HttpResponseRedirect(reverse('admin:index'))


Answered By - Aylen
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.