web-dev-qa-db-ja.com

Django RESTを含むフレームワークURL Django 2.0

Django REST Framework with Django 2.0プロジェクト、つまり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にアクセスすると、次のようになります。

ページが見つかりません(404)

urlpatternsはどのように設定すればよいですか?

8
HenryM
_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}
_
14
Ykh

routerを登録したら、それをurlpatternsに含める必要があります。 @Ykhが提案した方法は技術的には正しいですが、コンテンツに関しては要点がありません。

urlpatterns = [
    # here you include your router
    path('', include(router.urls)),
    # here you include the authentication paths
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]

これで、次のルートができます。

http://localhost:8000/regulations/
http://localhost:8000/languages/

プラス:

http://localhost:8000/api-auth/{other paths}
2
cezar