PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 19, 2022

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

 October 19, 2022     admin, authentication, django     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing