我仔细阅读了涉及此主题的其他几个问题,但是,没有描述include()的情况(包括另一个urls.py文件)。我还查看了1.11文档here并按照编码进行编码,但是,我不断得到错误“TypeError:在include()的情况下,视图必须是可调用的或列表/元组。”每个推导出这个和其他两个答案都无济于事。我的错误/误解在哪里?
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", {})
答案 0 :(得分:2)
错误不是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()
的参数。但是,在这种情况下,您应该完全从该网址格式中删除命名空间,并将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', {})
答案 1 :(得分:2)
您不应在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'),
]