Django — Conditional Login Redirect

Create a separate view that redirects user’s based on whether they are in the admin group.

from django.shortcuts import redirect

def login_success(request):
    """
    Redirects users based on whether they are in the admins group
    """
    if request.user.groups.filter(name="admins").exists():
        # user is an admin
        return redirect("admin_list")
    else:
        return redirect("other_view")

Add the view to your urls.py,

url(r'login_success/$', views.login_success, name="login_success")

then use it for your LOGIN_REDIRECT_URL setting.

LOGIN_REDIRECT_URL = 'login_success'

Leave a Comment