How to Redirect a User to Another Page Once Logged In with Django



Python


In this article, we show how to redirect a user to another page once logged in with Django.

So, say we've created a registration and log in page, where users can register and then log in to your website.

How can you redirect a user to another page once logged in?

In Django, this is very easy, as all we have to do is add a single line to the settings.py file.


settings.py File

So we go to the settings.py file and add in the following line, shown below.



All we have to do is add in the variable, LOGIN_REDIRECT_URL and set equal to whatever page on our website we want to redirect a user to once logged in.

This can be any page on your website.

In this example, after a user has logged in, the user gets redirect to the page, http://127.0.0.1:8000/home/

The value of this variable can either be set to the harcoded URL or it can be set equal to the name of the URL pattern.

Thus, we can also use the following line below.



So if the URL name is equal to home, this will work. This is the name attribute in the urls.py file.

So this is how a user can be redirect to another page once logged in with Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...