Redirects to the home page, the user who is already logged in and accidentally returns to the login page

Hello everyone.
I have a simple Django 5.0 project with three apps.
I use Django’s default authentication system.

I have a small problem that unfortunately I haven’t been able to solve myself yet.

I can’t make it so that if the user is already logged in, if the user returns to the login link, the user is automatically redirected to the home page.

I’ve tried searching the internet and documentation for Django 5.0, but I can’t figure out how to use it
“redirect_authenticated_user”.
And I’m not sure it’s the best system to adopt.

I’ve tried a thousand ways, but it doesn’t work.

How do I use “redirect_authenticated_user” correctly?
In which urls.py file (since I have more than two django apps)?
Is this the right thing to do even if I have class-based views that require certain permissions and there may be social media fingerprinting issues?

Thanks a lot to everyone.
I know these are trivial questions, but I’m truly a beginner.
I’m sorry I couldn’t do it on my own and I welcome any little advice I can get.

Thanks again to everyone.

The attribute redirect_authenticated_user applies to the LoginView class. It only has significance if you’re using the default Django LoginView CBV, or a login view class that inherits from it.

The complete answer to your question then depends upon how you’re logging in your users.

If you’re using the system-provided LoginView, you can set this attribute in the url definition.

If you’re using a subclass of it, you can set the attribute in the class, override the dispatch method, or set the attribute in the url definition - your choice of the three.

If you’re using a completely separate view to log people in, then you would handle this yourself as you see fit.

It’s worth looking at the source code in django.contrib.auth.views.LoginView to see how this is handled.

1 Like

Hi @KenWhitesell, thanks for your reply.
The problem is that I’m using Django’s automatic authentication system.
So I don’t have any LoginView to modify.
Are you saying it’s better to create a specific one?

The only thing I have that I can modify are the urls.py of my apps:

app1\urls.py

from django.urls import path, include
from . import views

urlpatterns = [    
     path('', views.base, name='base'),
     path('accounts/', include('django.contrib.auth.urls')),
]

Thanks Again.

First, and it’s a “perspective” or “mindset” that I suggest you adopt, nothing is “automatic”. Everything that happens, occurs because some code exists to make it happen.

In this particular case, you are using a LoginView - specifically, the one provided by Django.

Its URL is defined in the django.contrib.auth.urls file.

If you look at that file, you’ll find the following line:
path("login/", views.LoginView.as_view(), name="login"),

Now, we’ll take advantage of the knowledge that Django searches the urlpatterns sequentially, using the first entry matching the URL being requested, to override this entry.

So, instead of having:

You would now have:

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

You’ll also need to import the reference to LoginView in your urls.py file:
from django.contrib.auth.views import LoginView

Now that you’ve got a reference to the LoginView that you are using, you can alter its behavior by changing the redirect_authenticated_user attribute on that view by setting it in that url definition:
... , LoginView.as_view(redirect_authenticated_user=True), ...

2 Likes

A thousand thanks @KenWhitesell.
Now it works and it’s much clearer how it works.