URLs

Okay, now let's wire up the API URLs. On to tutorial/urls.py

  1. from django.urls import include, path
  2. from rest_framework import routers
  3. from tutorial.quickstart import views
  4. router = routers.DefaultRouter()
  5. router.register(r'users', views.UserViewSet)
  6. router.register(r'groups', views.GroupViewSet)
  7. # Wire up our API using automatic URL routing.
  8. # Additionally, we include login URLs for the browsable API.
  9. urlpatterns = [
  10. path('', include(router.urls)),
  11. path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
  12. ]

Because we're using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class.

Again, if we need more control over the API URLs we can simply drop down to using regular class-based views, and writing the URL conf explicitly.

Finally, we're including default login and logout views for use with the browsable API. That's optional, but useful if your API requires authentication and you want to use the browsable API.