diff options
| author | Tom Christie | 2014-12-28 11:24:49 +0000 | 
|---|---|---|
| committer | Tom Christie | 2014-12-28 11:24:49 +0000 | 
| commit | 8dc95ee22181de6e38c7187426bca9fcee9d7927 (patch) | |
| tree | b85cf07a261ead4efc89bbff460227ae0f8db8f6 /docs/api-guide | |
| parent | 7b42c5ed17a2430d66da88932ad4e81492d9b914 (diff) | |
| download | django-rest-framework-8dc95ee22181de6e38c7187426bca9fcee9d7927.tar.bz2 | |
Add notes on include and namespacing. Closes #2335.
Diffstat (limited to 'docs/api-guide')
| -rw-r--r-- | docs/api-guide/routers.md | 32 | 
1 files changed, 32 insertions, 0 deletions
| diff --git a/docs/api-guide/routers.md b/docs/api-guide/routers.md index 6819adb6..3a8a8f6c 100644 --- a/docs/api-guide/routers.md +++ b/docs/api-guide/routers.md @@ -49,6 +49,38 @@ This means you'll need to explicitly set the `base_name` argument when registeri  --- +### Using `include` with routers + +The `.urls` attribute on a router instance is simply a standard list of URL patterns. There are a number of different styles for how you can include these URLs. + +For example, you can append `router.urls` to a list of existing views… + +    router = routers.SimpleRouter() +    router.register(r'users', UserViewSet) +    router.register(r'accounts', AccountViewSet) +     +    urlpatterns = [ +        url(r'^forgot-password/$, ForgotPasswordFormView.as_view(), +    ] +     +    urlpatterns += router.urls + +Alternatively you can use Django's `include` function, like so… + +    urlpatterns = [ +        url(r'^forgot-password/$, ForgotPasswordFormView.as_view(), +        url(r'^', include(router.urls)) +    ] + +Router URL patterns can also be namespaces. + +    urlpatterns = [ +        url(r'^forgot-password/$, ForgotPasswordFormView.as_view(), +        url(r'^api/', include(router.urls, namespace='api')) +    ] + +If using namespacing with hyperlinked serializers you'll also need to ensure that any `view_name` parameters on the serializers correctly reflect the namespace. In the example above you'd need to include a parameter such as `view_name='api:user-detail'` for serializer fields hyperlinked to the user detail view. +  ### Extra link and actions  Any methods on the viewset decorated with `@detail_route` or `@list_route` will also be routed. | 
