Skip to content

Commit

Permalink
Added trailing_slash argument to routers. Closes #905
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Jun 4, 2013
1 parent ffa27b8 commit f1251e8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
13 changes: 12 additions & 1 deletion docs/api-guide/routers.md
Expand Up @@ -66,6 +66,13 @@ This router includes routes for the standard set of `list`, `create`, `retrieve`
<tr><td>POST</td><td>@action decorated method</td></tr>
</table>

By default the URLs created by `SimpleRouter` are appending with a trailing slash.
This behavior can be modified by setting the `trailing_slash` argument to `False` when instantiating the router. For example:

router = SimpleRouter(trailing_slash=False)

Trailing slashes are conventional in Django, but are not used by default in some other frameworks such as Rails. Which style you choose to use is largely a matter of preference, although some javascript frameworks may expect a particular routing style.

## DefaultRouter

This router is similar to `SimpleRouter` as above, but additionally includes a default API root view, that returns a response containing hyperlinks to all the list views. It also generates routes for optional `.json` style format suffixes.
Expand All @@ -83,6 +90,10 @@ This router is similar to `SimpleRouter` as above, but additionally includes a d
<tr><td>POST</td><td>@action decorated method</td></tr>
</table>

As with `SimpleRouter` the trailing slashs on the URL routes can be removed by setting the `trailing_slash` argument to `False` when instantiating the router.

router = DefaultRouter(trailing_slash=False)

# Custom Routers

Implementing a custom router isn't something you'd need to do very often, but it can be useful if you have specific requirements about how the your URLs for your API are strutured. Doing so allows you to encapsulate the URL structure in a reusable way that ensures you don't have to write your URL patterns explicitly for each new view.
Expand All @@ -91,7 +102,7 @@ The simplest way to implement a custom router is to subclass one of the existing

## Example

The following example will only route to the `list` and `retrieve` actions, and unlike the routers included by REST framework, it does not use the trailing slash convention.
The following example will only route to the `list` and `retrieve` actions, and does not use the trailing slash convention.

class ReadOnlyRouter(SimpleRouter):
"""
Expand Down
1 change: 1 addition & 0 deletions docs/css/default.css
Expand Up @@ -301,4 +301,5 @@ td, th {

table {
border-color: white;
margin-bottom: 0.6em;
}
17 changes: 12 additions & 5 deletions rest_framework/routers.py
Expand Up @@ -18,7 +18,6 @@
from collections import namedtuple
from rest_framework import views
from rest_framework.compat import patterns, url
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.urlpatterns import format_suffix_patterns
Expand Down Expand Up @@ -72,7 +71,7 @@ class SimpleRouter(BaseRouter):
routes = [
# List route.
Route(
url=r'^{prefix}/$',
url=r'^{prefix}{trailing_slash}$',
mapping={
'get': 'list',
'post': 'create'
Expand All @@ -82,7 +81,7 @@ class SimpleRouter(BaseRouter):
),
# Detail route.
Route(
url=r'^{prefix}/{lookup}/$',
url=r'^{prefix}/{lookup}{trailing_slash}$',
mapping={
'get': 'retrieve',
'put': 'update',
Expand All @@ -95,7 +94,7 @@ class SimpleRouter(BaseRouter):
# Dynamically generated routes.
# Generated using @action or @link decorators on methods of the viewset.
Route(
url=r'^{prefix}/{lookup}/{methodname}/$',
url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$',
mapping={
'{httpmethod}': '{methodname}',
},
Expand All @@ -104,6 +103,10 @@ class SimpleRouter(BaseRouter):
),
]

def __init__(self, trailing_slash=True):
self.trailing_slash = trailing_slash and '/' or ''
super(SimpleRouter, self).__init__()

def get_default_base_name(self, viewset):
"""
If `base_name` is not specified, attempt to automatically determine
Expand Down Expand Up @@ -193,7 +196,11 @@ def get_urls(self):
continue

# Build the url pattern
regex = route.url.format(prefix=prefix, lookup=lookup)
regex = route.url.format(
prefix=prefix,
lookup=lookup,
trailing_slash=self.trailing_slash
)
view = viewset.as_view(mapping, **route.initkwargs)
name = route.name.format(basename=basename)
ret.append(url(regex, view, name=name))
Expand Down
35 changes: 32 additions & 3 deletions rest_framework/tests/test_routers.py
Expand Up @@ -50,7 +50,7 @@ def test_link_and_action_decorator(self):
route = decorator_routes[i]
# check url listing
self.assertEqual(route.url,
'^{{prefix}}/{{lookup}}/{0}/$'.format(endpoint))
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'.format(endpoint))
# check method to function mapping
if endpoint == 'action3':
methods_map = ['post', 'delete']
Expand Down Expand Up @@ -103,7 +103,7 @@ def test_custom_lookup_field_route(self):

def test_retrieve_lookup_field_list_view(self):
response = self.client.get('/notes/')
self.assertEquals(response.data,
self.assertEqual(response.data,
[{
"url": "http://testserver/notes/123/",
"uuid": "123", "text": "foo bar"
Expand All @@ -112,10 +112,39 @@ def test_retrieve_lookup_field_list_view(self):

def test_retrieve_lookup_field_detail_view(self):
response = self.client.get('/notes/123/')
self.assertEquals(response.data,
self.assertEqual(response.data,
{
"url": "http://testserver/notes/123/",
"uuid": "123", "text": "foo bar"
}
)


class TestTrailingSlash(TestCase):
def setUp(self):
class NoteViewSet(viewsets.ModelViewSet):
model = RouterTestModel

self.router = SimpleRouter()
self.router.register(r'notes', NoteViewSet)
self.urls = self.router.urls

def test_urls_have_trailing_slash_by_default(self):
expected = ['^notes/$', '^notes/(?P<pk>[^/]+)/$']
for idx in range(len(expected)):
self.assertEqual(expected[idx], self.urls[idx].regex.pattern)


class TestTrailingSlash(TestCase):
def setUp(self):
class NoteViewSet(viewsets.ModelViewSet):
model = RouterTestModel

self.router = SimpleRouter(trailing_slash=False)
self.router.register(r'notes', NoteViewSet)
self.urls = self.router.urls

def test_urls_can_have_trailing_slash_removed(self):
expected = ['^notes$', '^notes/(?P<pk>[^/]+)$']
for idx in range(len(expected)):
self.assertEqual(expected[idx], self.urls[idx].regex.pattern)

0 comments on commit f1251e8

Please sign in to comment.