python - 类型错误:在 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
解决方案
然后,您需要导入模块,例如:
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
.
推荐阅读
- python - 未检索数据 sqlite3 Python
- ansible - 由于来自 cisco 的密码错误,无法使用 ansible 登录 ssh
- node.js - JSON 文件中的随机对象
- python - FastApi 用户身份验证,无法添加额外字段进行注册
- python-3.x - 从pdf中提取纯文本的问题
- node.js - 你能在反应原生的同时使用nodejs吗
- c - 边缘检测不起作用!二维像素数组中的最后一个下标没有正确显示
- java - 给定来自特定日期数据的无日期时间戳列表,我应该如何检查日期是否在 00:00 提前?
- c# - 如何序列化具有自定义 IComparer 的 SortedDictionaty
- xcode - Settings.bundle Swift 5