ImportError: cannot import name 'login' from 'django.contrib.auth.views' (E:\pyt
hon\learning_log\ll_env\lib\site-packages\django\contrib\auth\views.py)
解答:
14
从Django的1.11,则login,logout等基于功能的视图已改写为基于类的视图:LoginView[Django的DOC]和LogoutView类,如在指定发布说明。仍然可以使用“旧的”基于函数的视图,但标记为已弃用。
原文为:
from django.conf.urls import url
from django.contrib.auth.views import login
from django.urls import re_path
from . import views
app_name ="users"
urlpatterns = [
re_path(r'^login/$', login, {"template_name" : 'users/login.html'},
name='login'),
]
改为:
from django.conf.urls import url
from django.contrib.auth.views import LoginView
from django.urls import re_path
from . import views
app_name ="users"
urlpatterns = [
re_path(r'^login/$', LoginView.as_view(template_name='users/login.html'),
name='login'),
]
问题二:
File "E:\python\learning_log\ll_env\lib\site-packages\django\core
e.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback
File "E:\python\learning_log\users\views.py", line 16, in registe
form = UserCreationForm()
NameError: name 'UserCreationForm' is not defined
[24/Dec/2018 14:32:04] "GET /users/register/ HTTP/1.1" 500 66835
解决:
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm