首页 > 解决方案 > 如何在 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')),
]

标签: djangodjango-rest-framework

解决方案


您应该router.urls直接传递,而不是作为字符串

path('', include(router.urls)),

https://www.django-rest-framework.org/api-guide/routers/#using-include-with-routers


推荐阅读