这是我的代码:
from rest_framework import routers
from rest_framework.routers import DefaultRouter
from .api import ListViewSet, CardViewSet
router = DefaultRouter()
router.register(r'lists', ListViewSet,'lists')
router.register(r'cards', CardViewSet, 'cards')
urlpatterns = router.urls
但是当我运行服务器时出现了这个错误:
Page not found (404) Request Method:
GET Request URL: http://localhost:8000/scrumboard/cards Using the URLconf defined in djangularApp.urls, Django tried these URL patterns, in this order:
^admin/ ^$ ^scrumboard ^lists/$ [name='lists-list'] ^scrumboard
^lists\.(?P<format>[a-z0-9]+)/?$ [name='lists-list'] ^scrumboard
^lists/(?P<pk>[^/.]+)/$ [name='lists-detail'] ^scrumboard
^lists/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$ [name='lists-detail']
^scrumboard ^cards/$ [name='cards-list'] ^scrumboard
^cards\.(?P<format>[a-z0-9]+)/?$ [name='cards-list'] ^scrumboard
^cards/(?P<pk>[^/.]+)/$ [name='cards-detail'] ^scrumboard
^cards/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$ [name='cards-detail']
^scrumboard ^$ [name='api-root'] ^scrumboard
^\.(?P<format>[a-z0-9]+)/?$ [name='api-root']
The current path, scrumboard/cards, didn't match any of these.
你看到这个错误,因为你的Django设置文件中有DEBUG = True。将其更改为False,Django将显示标准的404页面。
看起来你忘记在scrumboard
网址后添加/,可能包含路由器的网址。这是可见的,因为^scrumboard ^cards/$
应该是^scrumboard/ ^cards/$
以匹配您期望的URL。
你好像没有在你的project/urls.py
注册网址
path('api/', include((router.urls), namespace='api')),
这意味着路径将从/api/cards
开始
或者为/scrumboard
path('scrumboard/', include((router.urls), namespace='scrumboard')),
然后你可以使用/scrumboard/cards