首页 > 解决方案 > 类型错误:在 include().django2.2.2 的情况下,视图必须是可调用的或列表/元组

问题描述

我正在尝试使用urls.py以下代码制作我的第一个应用程序:

from django.contrib import admin
from django.urls import include, path


urlpatterns = [
    path('', 'blogs.views.home', name='home'),
    path('admin/', include(admin.site.urls)),
]

`

我得到这个错误TypeError: view must be a callable or a list/tuple in the case of include()

当我不使用引号时,例如:

from django.contrib import admin
from django.urls import include, path


urlpatterns = [
    path('', blogs.views.home, name='home'),
    path('admin/', include(admin.site.urls)),
]

我收到以下错误NameError: name 'blogs' is not defined

标签: pythondjangotypeerrorcallable

解决方案


然后,您需要导入模块,例如:

from django.contrib import admin
from django.urls import include, path
import blogs.views


urlpatterns = [
    path('', blogs.views.home, name='home'),
    path('admin/', include(admin.site.urls)),
]

因为你使用了一个标识符,所以它需要在范围内。如果您使用字符串文字,Django 将使用importlib.


推荐阅读