Skip to content

Commit 8dc95ee

Browse files
Add notes on include and namespacing. Closes encode#2335.
1 parent 7b42c5e commit 8dc95ee

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

docs/api-guide/routers.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,38 @@ This means you'll need to explicitly set the `base_name` argument when registeri
4949

5050
---
5151

52+
### Using `include` with routers
53+
54+
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.
55+
56+
For example, you can append `router.urls` to a list of existing views…
57+
58+
router = routers.SimpleRouter()
59+
router.register(r'users', UserViewSet)
60+
router.register(r'accounts', AccountViewSet)
61+
62+
urlpatterns = [
63+
url(r'^forgot-password/$, ForgotPasswordFormView.as_view(),
64+
]
65+
66+
urlpatterns += router.urls
67+
68+
Alternatively you can use Django's `include` function, like so…
69+
70+
urlpatterns = [
71+
url(r'^forgot-password/$, ForgotPasswordFormView.as_view(),
72+
url(r'^', include(router.urls))
73+
]
74+
75+
Router URL patterns can also be namespaces.
76+
77+
urlpatterns = [
78+
url(r'^forgot-password/$, ForgotPasswordFormView.as_view(),
79+
url(r'^api/', include(router.urls, namespace='api'))
80+
]
81+
82+
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.
83+
5284
### Extra link and actions
5385

5486
Any methods on the viewset decorated with `@detail_route` or `@list_route` will also be routed.

0 commit comments

Comments
 (0)