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中文網其他相關文章!