From 93d568c7d44d6ddfa777f7100a082ab687ddc388 Mon Sep 17 00:00:00 2001 From: Artem Homenko Date: Mon, 6 Feb 2017 15:08:34 +0200 Subject: [PATCH] Ability to use Router with empty path with and without trailing_slash --- rest_framework/routers.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/rest_framework/routers.py b/rest_framework/routers.py index 027a78cc1..80a56e42f 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -96,13 +96,13 @@ class SimpleRouter(BaseRouter): # Generated using @list_route decorator # on methods of the viewset. DynamicListRoute( - url=r'^{prefix}/{methodname}{trailing_slash}$', + url=r'^{prefix}{methodname}{trailing_slash}$', name='{basename}-{methodnamehyphen}', initkwargs={} ), # Detail route. Route( - url=r'^{prefix}/{lookup}{trailing_slash}$', + url=r'^{prefix}{lookup}{trailing_slash}$', mapping={ 'get': 'retrieve', 'put': 'update', @@ -115,7 +115,7 @@ class SimpleRouter(BaseRouter): # Dynamically generated detail routes. # Generated using @detail_route decorator on methods of the viewset. DynamicDetailRoute( - url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$', + url=r'^{prefix}{lookup}/{methodname}{trailing_slash}$', name='{basename}-{methodnamehyphen}', initkwargs={} ), @@ -247,11 +247,18 @@ class SimpleRouter(BaseRouter): continue # Build the url pattern + if prefix: + prefix += "/" + regex = route.url.format( prefix=prefix, lookup=lookup, trailing_slash=self.trailing_slash ) + + if regex == '^/$': + regex = '^$' + view = viewset.as_view(mapping, **route.initkwargs) name = route.name.format(basename=basename) ret.append(url(regex, view, name=name))