django - 如何在 django 和 DRF 中使用“路由器”
问题描述
我想在 django 中使用路由器。但是当我尝试导入模块并迁移它以使用路由器时,出现以下错误。
ModuleNotFoundError:没有名为“路由器”的模块
我确实导入了模块,但我无法理解没有模块的错误。你能给我一个解决方案吗?这是我的代码。
网址.py
from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from api.views import arduinoViewSet
router = routers.DefaultRouter()
router.register('arduino', arduinoViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('router.urls')),
path('auth/', include('rest_auth.urls')),
path('auth/registration/', include('rest_auth.registration.urls')),
]
解决方案
您应该router.urls
直接传递,而不是作为字符串
path('', include(router.urls)),
https://www.django-rest-framework.org/api-guide/routers/#using-include-with-routers
推荐阅读
- javascript - 使用 Typescript 泛型类型
- plot - 如何为 highchart 添加数据标签?
- sql - SQL 返回项目的排名
- google-cloud-platform - GCP 管理多个云功能流的最佳方式
- azure - 在 Durable Functions 中等待外部事件
- python - 无法从线性函数中获得正确的坐标
- python - 熊猫:绘图时忽略索引中的年份,但保留小时/日/月
- python - VS & Anaconda 启动器
- scala - Akka Stream, Tcp().bind, 客户端关闭套接字时的处理
- assembly - 在 MIPS 中加载和存储值的问题