Django.contrib.auth import views || Login Authentication and url redirection

Asked

Viewed 486 times

0

I’m trying to use Django’s libraries to check if the user is logged in, and only then can he log into the pages of my website. I’m coding according to some Internet classes, in his example was functional, and he showed an error, asking register/login.html, then he created directory template/register/login.html so to check if the user is logged in, and if not, open a login screen.

I appreciate any kind of help, indication, or improvement in my code.

Main folder of the project, py..

from django.contrib import admin
from django.urls import path,include
from clientes import urls as clientes_urls
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('clientes/', include(clientes_urls)),
    path('login/', auth_views.auth_login, name='login'),
    path('logout/', auth_views.auth_logout, name='logout'),
    path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

py Settings. inserir a descrição da imagem aqui

#Templates

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Error screen: inserir a descrição da imagem aqui

Edit 1: There is none def, in the py views., calling any 'login' directory (either by html, function or Django library), both in my code and in the code I am basing myself on.

1 answer

0


path('login/', auth_views.LoginView.as_view(),name='login'),

My problem was with the version of Django, when using the code in this way it works the way I wanted it to.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.