Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django cannot import login from django.contrib.auth.views

I try to build a login function for my page. To edit the urls.py as followed, it keeps printing this:

cannot import name 'login' from 'django.contrib.auth.views'

How could I deal with the problem?

from django.contrib.auth.views import login
from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
    path("login/", 
         login, 
         {"template_name": "users/login.html"}, 
         name="login"),
]
like image 433
Thomas.Q Avatar asked Aug 18 '18 07:08

Thomas.Q


2 Answers

Since django-1.11, the login, logout, etc. function-based views have been rewritten to class-based views: the LoginView [Django-doc] and LogoutView [Django-doc] classes, as is specified in the release notes. The "old" function-based views could still be used, but were marked as deprecated.

In django-2.1, the old function-based views have been removed, as specified in the release notes.

You can write it like:

from django.contrib.auth.views import LoginView

from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
    path('login/', 
        LoginView.as_view(
            template_name='users/login.html'
        ), 
        name="login"
    ),
]
like image 114
Willem Van Onsem Avatar answered Nov 16 '22 04:11

Willem Van Onsem


try this

app_name = 'users'

urlpatterns = [
    url(r'^login/$', LoginView.as_view(template_name='users/login.html'), name='login'),
]
like image 3
Amit Patel Avatar answered Nov 16 '22 02:11

Amit Patel