Categories
Python Answers

How to redirect logged in users from login page with Python Django?

Spread the love

Sometimes, we want to redirect logged in users from login page with Python Django.

In this article, we’ll look at how to redirect logged in users from login page with Python Django.

How to redirect logged in users from login page with Python Django?

To redirect logged in users from login page with Python Django, we can set the redirect_authenticated_user argument when we call as_view.

For instance, we write

from django.contrib.auth import views as auth_views
from django.urls import path

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'),
]

to call auth_views.LoginView.as_view with the redirect_authenticated_user to True to redirect users from the login page to the destination once the user is logged in.

Conclusion

To redirect logged in users from login page with Python Django, we can set the redirect_authenticated_user argument when we call as_view.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *