django rest framework: routers.DefaultRouter() url

2019-08-05 16:49发布

问题:

I want to define a path to access a certain api. What works so far (urls.py):

router = routers.DefaultRouter()
router.register(r'test', views.TestViewSet)
urlpatterns = patterns('',
    url(r'^api/', include(router.urls)),
)

What i would like to do is adding a new ViewSet which provides "subfunctionality" of test (urls.py):

router.register(r'test/add', views.TestNewViewSet)

But this does not work. When accessing this api, all i get is a "404 Not Found" error. No exceptions are thrown when accessing the api. So what is wrong?

Any help is appreciated!

回答1:

Try with

urlpatterns = patterns('',
url(r'^api/', include(router.urls)),
url(r'^test/add/$',  TestNewViewSet.as_view(), name='whatever'),

)