的Django == 1.10.5 登录。 我已经登录表单,然后编写这些代码并想运行服务器
Urls.py:
urlpatterns = [
#previous login view
#url(r'^login/$', views.user_login, name='login'),
#login/logout urls
url(r'^$', views.dashboard, name='dashboard'),
url(r'^login/$', 'login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^logout-thenlogin/$','django.contrib.auth.views.logout_then_login',
name='logout_then_login'),
]
Views.py:
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from .forms import LoginForm
def user_login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
user = authenticate(username=cd['username'], password=cd['password'])
if user is not None:
if user.is_active:
login(request, user)
return HttpResponse('Authenticated successfully')
else:
return HttpResponse('Disabled account')
else:
return HttpResponse('Invalid login')
else:
form = LoginForm()
return render(request, 'account/login.html', {'form': form})
@login_required
def dashboard(request):
return render(request, 'account/dashboard.html', {'section': 'dashboard'})
当我想要运行服务器时 - 我收到了这条消息: 在包含
的情况下,视图必须是可调用的或列表\ tuple答案 0 :(得分:1)
Django 1.10+不再允许您在网址格式中将视图指定为字符串(例如“myapp.views.home”)。
解决方案是更新您的urls.py以包含可调用的视图。这意味着您必须在urls.py中导入视图。如果您的URL模式没有名称,那么现在是添加名称的好时机,因为使用虚线python路径进行反转不再有效。
urlpatterns = [
#previous login view
#url(r'^login/$', views.user_login, name='login'),
#login/logout urls
url(r'^$', views.dashboard, name='dashboard'),
url(r'^login/$', your_app_name.views.user_login, name='login'),
url(r'^logout/$', django.contrib.auth.views.logout, name='logout'),
url(r'^logout-thenlogin/$', django.contrib.auth.views.logout_then_login,
name='logout_then_login'),
]