Python 姜戈 |在 include() 的情况下,视图必须是可调用的或 list/tuple
Python Django | view must be a callable or a list/tuple in the case of include()
File "\app\urls.py", line 10, in
name='login'),
File "\django\conf\urls__init__.py", line 85, in url
raise TypeError('view must be a callable or a list/tuple in the case of include().') TypeError: view must be a callable or a
list/tuple in the case of include().
urls.py
urlpatterns = [
# previous login view
# url(r'^login/$', views.user_login, name='login'),
# login / logout urls
url(r'^login/$',
'django.contrib.auth.views.login',
name='login'),
url(r'^logout/$',
'django.contrib.auth.views.logout',
name='logout'),
url(r'^logout-then-login/$',
'django.contrib.auth.views.logout_then_login',
name='logout_then_login'),
]
views.py
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth import authenticate, login
from .forms import LoginForm
from django.contrib.auth.decorators import login_required
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'})
字符串引用在 Django 1.10 中已弃用。
import django.contrib.auth.views
urlpatterns = [
...
url(r'^login/$', django.contrib.auth.views.login, name='login'),
...
]
File "\app\urls.py", line 10, in name='login'),
File "\django\conf\urls__init__.py", line 85, in url raise TypeError('view must be a callable or a list/tuple in the case of include().') TypeError: view must be a callable or a list/tuple in the case of include().
urls.py
urlpatterns = [ # previous login view # url(r'^login/$', views.user_login, name='login'), # login / logout urls url(r'^login/$', 'django.contrib.auth.views.login', name='login'), url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout'), url(r'^logout-then-login/$', 'django.contrib.auth.views.logout_then_login', name='logout_then_login'), ]
views.py
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth import authenticate, login
from .forms import LoginForm
from django.contrib.auth.decorators import login_required
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'})
字符串引用在 Django 1.10 中已弃用。
import django.contrib.auth.views
urlpatterns = [
...
url(r'^login/$', django.contrib.auth.views.login, name='login'),
...
]