2 votes

How to prevent them from entering the login once the session is started with Django

Hello, I have a simple problem but I don't know how to solve it, what happens is that I would like to know if there is a method or something that prevents the user from entering the login if they already have the session open.

 from django.contrib import admin
from django.urls import path, include
from django.contrib.auth.views import LogoutView, LoginView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls')),
    path('estudiante/', include('volder_app.urls')),
    path('', LoginView.as_view(template_name='registration/login.html'), name="login"), /*<------- */
    path('logout/', LogoutView.as_view(template_name='registration/logout.html'), name="logout"),
]

The version of Django I use is 2.2, and the only place I call the URL is from there, then just the login.html template

5voto

César González Points 348

You can simply use redirect_authenticated_user in as_view()

 from django.contrib import admin
from django.urls import path, include
from django.contrib.auth.views import LogoutView, LoginView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls')),
    path('estudiante/', include('volder_app.urls')),
    path('', LoginView.as_view(template_name='registration/login.html', redirect_authenticated_user=True), name="login"),
    path('logout/', LogoutView.as_view(template_name='registration/logout.html'), name="logout"),
]    

Remember to set the destination url to settings.LOGIN_REDIRECT_URL

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X