gpt4 book ai didi

带有Django 2.0的Django REST Framework URL

转载 作者:行者123 更新时间:2023-12-04 05:28:32 25 4
gpt4 key购买 nike

我正在尝试使用Django 2.0项目设置Django REST Framework,这意味着url(r'^something/' ...已替换为path(something/ ...

我正在尝试找出如何设置rest_framework模式。

这就是我所拥有的:

router = routers.DefaultRouter()
router.register(r'regulations', api.RegulationViewSet)
router.register(r'languages', api.LanguageViewSet)


urlpatterns = [
...
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
...
]

如果我去 http://127.0.0.1:8000/regulations我只会得到:

Page not found (404)



我应该如何设置 urlpatterns

最佳答案

urlpatterns = [
...
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
...
]

使用 path('', include(router.urls)),,您可以获得:
http://127.0.0.1:8000/regulations/
http://127.0.0.1:8000/languages/


path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),

你可以得到:
http://127.0.0.1:8000/api-auth/{other paths}

关于带有Django 2.0的Django REST Framework URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49005264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com