TypeError: view must be a callable or a list/tuple in the case of include()
当视图被调用时会遇到此错误升级到 Django 1.10 后指定为 URL 模式中的字符串。 Django 1.10 要求将视图指定为可调用视图。
解决方案:
要解决此错误,请更新 urls.py 以包含可调用视图。
单视图导入:
在 urls.py 中导入单个视图并将其指定为函数引用:
<code class="python">from django.conf.urls import include, url from myapp.views import home, contact urlpatterns = [ url(r'^$', home, name='home'), url(r'^contact/$', contact, name='contact'), ]</code>
多视图导入:
为了避免单独导入每个视图,您可以从应用程序导入整个视图模块:
<code class="python">from django.conf.urls import include, url from myapp import views as myapp_views urlpatterns = [ url(r'^$', myapp_views.home, name='home'), url(r'^contact/$', myapp_views.contact, name='contact'), ]</code>
使用“as”关键字:
为防止从不同应用导入多个视图模块时发生名称冲突,请使用“as”关键字:
<code class="python">from django.conf.urls import include, url from django.contrib.auth import views as auth_views from myapp import views as myapp_views urlpatterns = [ url(r'^$', myapp_views.home, name='home'), url(r'^contact/$', myapp_views.contact, name='contact'), url(r'^login/$', auth_views.login, name='login'), ]</code>
以上是如何修复 Django 1.10 中的'类型错误:视图必须是可调用的”?的详细内容。更多信息请关注PHP中文网其他相关文章!