From 73c38b0bcbc8cb51b383e9c10b3a0e4ed87ae74d Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Sun, 21 Dec 2014 22:21:14 +0200 Subject: [PATCH 1/3] Added more common usage examples of including routers in urlpatterns. This is an alternative to #2333. --- docs/api-guide/routers.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/api-guide/routers.md b/docs/api-guide/routers.md index 6819adb6a..2512c6bab 100644 --- a/docs/api-guide/routers.md +++ b/docs/api-guide/routers.md @@ -21,6 +21,32 @@ Here's an example of a simple URL conf, that uses `SimpleRouter`. router.register(r'accounts', AccountViewSet) urlpatterns = router.urls +A more common usage of routers is + + from django.conf.urls import patterns, include, url + from rest_framework import routers + + router = routers.SimpleRouter() + router.register(r'users', UserViewSet) + router.register(r'accounts', AccountViewSet) + + urlpatterns = patterns('', + url(r'^', include(router.urls)) + ) + +It is possible to namespace the router's views using Django's standard namespacing mechanism: + + from django.conf.urls import patterns, include, url + from rest_framework import routers + + router = routers.SimpleRouter() + router.register(r'users', UserViewSet) + router.register(r'accounts', AccountViewSet) + + urlpatterns = patterns('', + url(r'^', include(router.urls, namespace='api')) + ) + There are two mandatory arguments to the `register()` method: * `prefix` - The URL prefix to use for this set of routes. From 670a78536f80e5d80028c610d52b973ba9eba3e3 Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Mon, 22 Dec 2014 11:35:15 +0200 Subject: [PATCH 2/3] Clarified intention of the first code example. --- docs/api-guide/routers.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/api-guide/routers.md b/docs/api-guide/routers.md index 2512c6bab..4e6fa16d1 100644 --- a/docs/api-guide/routers.md +++ b/docs/api-guide/routers.md @@ -21,7 +21,7 @@ Here's an example of a simple URL conf, that uses `SimpleRouter`. router.register(r'accounts', AccountViewSet) urlpatterns = router.urls -A more common usage of routers is +A more common usage of routers is to include it in order to combine it with other patterns. from django.conf.urls import patterns, include, url from rest_framework import routers @@ -31,6 +31,7 @@ A more common usage of routers is router.register(r'accounts', AccountViewSet) urlpatterns = patterns('', + url(r'^forgot-password/$, ForgotPasswordFormView.as_view(), url(r'^', include(router.urls)) ) From bf7309ef72428c12889327178d5341aa3b6b1b86 Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Wed, 24 Dec 2014 10:58:26 +0200 Subject: [PATCH 3/3] Adjusted the documentation according to comments. --- docs/api-guide/routers.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/api-guide/routers.md b/docs/api-guide/routers.md index 4e6fa16d1..708b521e6 100644 --- a/docs/api-guide/routers.md +++ b/docs/api-guide/routers.md @@ -21,32 +21,33 @@ Here's an example of a simple URL conf, that uses `SimpleRouter`. router.register(r'accounts', AccountViewSet) urlpatterns = router.urls -A more common usage of routers is to include it in order to combine it with other patterns. +A more common usage of routers is to append it in order to combine it with other patterns. - from django.conf.urls import patterns, include, url + from django.conf.urls import url from rest_framework import routers router = routers.SimpleRouter() router.register(r'users', UserViewSet) router.register(r'accounts', AccountViewSet) - urlpatterns = patterns('', + urlpatterns = [ url(r'^forgot-password/$, ForgotPasswordFormView.as_view(), - url(r'^', include(router.urls)) - ) + ] + + urlpatterns += router.urls It is possible to namespace the router's views using Django's standard namespacing mechanism: - from django.conf.urls import patterns, include, url + from django.conf.urls import include, url from rest_framework import routers router = routers.SimpleRouter() router.register(r'users', UserViewSet) router.register(r'accounts', AccountViewSet) - urlpatterns = patterns('', + urlpatterns = [ url(r'^', include(router.urls, namespace='api')) - ) + ] There are two mandatory arguments to the `register()` method: