The login page is displayed even if the user is already registered

I use the Django authentication view django.contrib.auth.views.loginto log in my users.

urls.py

urlpatterns = patterns('',
    url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
)

Here is a document regarding this functionality.

My problem: The login page is displayed even if the user is already connected.

+4
source share
2 answers

You can simply use the account login tab with your own changes in your own view. Just change the login URL to point to your own view, and then check if they are logged in:


views.py

from django.contrib.auth.views import login as contrib_login

def login(request):
    if request.user.is_authenticated():
        return redirect(settings.LOGIN_REDIRECT_URL)
    return contrib_login(request)
0

Django 2.x

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'),
]
0

Source: https://habr.com/ru/post/1527368/


All Articles