TypeError: view must be a callable or a list/tuple when including another urls.py

TypeError: view must be a callable or a list/tuple when including another urls.py

我已经仔细阅读了涵盖该主题的其他几个问题,但是,none 描述了 include() 的情况(包括另一个 urls.py 文件)。我还查看了 1.11 文档 here 并根据代码进行了编码,但是,我不断收到错误消息“TypeError:view must be a callable or a list/tuple in case of include()”。尝试了这个和其他两个答案的几乎所有推导都无济于事。我的 mistake/misunderstanding 在哪里?

urls.py

from django.contrib import admin
from django.conf.urls import include, url

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^atfl/', include('atfl.urls'), namespace="atfl"),
]

atfl/urls.py

中的代码
from django.conf.urls import url
from atfl.views import home, people

urlpatterns = [
    url(r'^$', 'home', name='home'),
    url(r'^people/$', 'people', name='people'),
]

atfl/views.py

中的代码
from django.shortcuts import render_to_response

def index(request):
    return render_to_response('atfl/home.html', {})

def LoadTextFile(request):
    return render_to_response("atfl/people.html", {})

错误不是来自 include,而是来自您试图包含的 urls.py 中的字符串 'home''people'。使用您已经导入的视图:

from atfl.views import home, people

app_name = 'atfl'

urlpatterns = [
    url(r'^$', home, name='home'),
    url(r'^people/$', people, name='people'),
]

一旦你修复了它,你的 include 中就有一个你应该修复的错误。命名空间是 include 的参数,即 include('atfl.urls', namespace='atfl')。您将其作为 url() 的参数。但是,在这种情况下,您应该从 URL 模式中完全删除命名空间,并像上面那样将 app_name 添加到应用程序的 urls.py。

url(r'^atfl/', include('atfl.urls')),

最后,不要使用render_to_response。它已经过时了。请改用 render

from django.shortcuts import render_to_response

def index(request):
    return render(request, 'atfl/home.html', {})

你不应该在 atfl/urls.py:

中使用字符串
from django.conf.urls import url
from atfl.views import home, people

urlpatterns = [
    url(r'^$', home, name='home'),
    url(r'^people/$', people, name='people'),
]