Cannot import name 'login' in Django

Viewed 4929

I am currently working on a project in Django... I am trying to import login from django.contrib.auth.views but I get the following error:

ImportError: cannot import name 'login'

Here is my urls.py:

from django.conf.urls import url
from . import views
from django.contrib.auth.views import login

urlpatterns = [
    url('', views.home),
    url('login', login, {'template_name': 'accounts/login.html'})
]

error message(cmd):

ImportError: cannot import name 'login'
1 Answers

The old function-based authentication views, including login, were removed in Django 2.1. In Django 1.11+ you should switch to the new class-based authentication views, including LoginView.

I suggest you also switch to use the new path() instead of url().

from django.urls import path
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('', views.home),
    path('login', auth_views.LoginView.as_view(template_name='accounts/login.html')),
]

If you want to stick with url() or re_path(), then make sure you use ^ and $ to begin and end your regexes:

url(r'^$', views.home),
url(r'^login$', auth_views.LoginView.as_view(template_name='accounts/login.html')))
Related