Redirect to the previous page after login in Django

Photo by Brooke Lark on Unsplash

Redirect to the previous page after login in Django

Using class based view and Django authentication system

ยท

3 min read

Django implements numerous features to simplify your development experience. In this article, I want to demonstrate a simple behavior that you may find useful: redirecting the user to the exact page they were on before the login action. This could help many users avoid starting again from square one after logging in.

With the authentication system, many common behaviors are already implemented, but you can customize them according to your specific needs.


Understanding Django Authentication

Django simplifies the integration of authentication in your web application. Out of the box, Django offers a set of common URLs that cover various authentication functionalities. When you include django.contrib.auth.urls in your url.py these are the default URLs :

accounts/login/ [name='login']
accounts/logout/ [name='logout']
accounts/password_change/ [name='password_change']
accounts/password_change/done/ [name='password_change_done']
accounts/password_reset/ [name='password_reset']
accounts/password_reset/done/ [name='password_reset_done']
accounts/reset/<uidb64>/<token>/ [name='password_reset_confirm']
accounts/reset/done/ [name='password_reset_complete']

How redirect works

The goal is to insert inside your URL a parameter that contains a specific URL. A parameter is a string with a key and a value inside a URL. This key-value pair is readable with a GET method.

The value is the URL we want to redirect after a login action. You can think of this operation as the same when in your view you use redirect().

Customization

If you want to customize only a specific authentication behavior, such as the login process, Django makes it easy. In your urls.py file, include the following code:

# urls.py
from django.contrib.auth import views as auth_views

urlpatterns = [
    path("accounts/login/", auth_views.LoginView.as_view(), name="login"),
    path("accounts/", include("django.contrib.auth.urls")),
]

This modification introduces the use of LoginView from contrib.auth.urls. For simplicity, I renamed it using the alias auth_views.

๐Ÿ’ก
Note: position is really important, LoginView comes before including all the other urls using django.contrib.auth.url because it changes the override default behavior.

Inside templates

Now, let's move on to the templates. Inside your nav.html file, you can update the login link as follows:

<!-- templates/nav.html -->
...
<a href="{% url 'login'%}?next={{request.path}}"> 
    Login
</a>

This creates a link to the login page while preserving the user's intended destination using the next field in the GET request.

What if

With authentication built into Django, you can customize it. Let's explore a couple of possible scenarios:

Is the next field occupied?

In your project you may already be using next for other purposes. No problem, you could just specify redirect_field_name to search a different field, for example, "page".

# urls.py
from django.contrib.auth import views as auth_views

urlpatterns = [
    path(
        "account/login/",
        auth_views.LoginView.as_view(redirect_field_name="page"),
        name="login",
    ),
...
]

Inside your link to login you need to specify the field "page" like this:

<!-- templates/nav.html -->
<a href="{% url 'login'%}?page={{request.path}}"> 
    Login
</a>

Can I redirect directly to the Hompage?

In this scenario, you need to update your settings.py to insert your homepage URL. There's no need to update your urls.py file; simply include the authentication URLs, as we previously discussed.

# settings.py
...
LOGIN_REDIRECT_URL = "/"
...

Conclusion

With the Django authentication system, you can achieve your goals more easily. Learn how to customize specific authentication actions, like login, and take advantage of class-based views so you can adapt your application to fit your unique needs.

Resources

See the documentation for:

ย