Python Django |对于include(),view必须是callable或list / tuple

时间:2016-09-15 06:48:56

标签: python django login django-urls django-authentication

  

文件“\ app \ urls.py”,第10行,中       命名= '登录'),
  在网址中输入“\ django \ conf \ urls__init __。py”,第85行       提出TypeError('视图必须是可调用的或者在include()的情况下是列表/元组。')TypeError:view必须是可调用的或者是   include()的情况下的list / tuple。

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'})

1 个答案:

答案 0 :(得分:3)

Django 1.10中不推荐使用字符串引用。

import django.contrib.auth.views

urlpatterns = [
    ...
    url(r'^login/$', django.contrib.auth.views.login, name='login'),
    ...
]