mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 04:50:12 +03:00
Check for empty router prefix; adjust URL accordingly
It's easiest to fix this issue after we have made the regex. To try to fix it before would require doing something different for List vs Detail, which means we'd have to know which type of url we're constructing before acting accordingly.
This commit is contained in:
parent
a556b9cb42
commit
a372a8edea
|
@ -83,6 +83,7 @@ class BaseRouter(object):
|
|||
|
||||
|
||||
class SimpleRouter(BaseRouter):
|
||||
|
||||
routes = [
|
||||
# List route.
|
||||
Route(
|
||||
|
@ -258,6 +259,13 @@ class SimpleRouter(BaseRouter):
|
|||
trailing_slash=self.trailing_slash
|
||||
)
|
||||
|
||||
# If there is no prefix, the first part of the url is probably
|
||||
# controlled by project's urls.py and the router is in an app,
|
||||
# so a slash in the beginning will (A) cause Django to give
|
||||
# warnings and (B) generate URLS that will require using '//'.
|
||||
if not prefix and regex[:2] == '^/':
|
||||
regex = '^' + regex[2:]
|
||||
|
||||
view = viewset.as_view(mapping, **route.initkwargs)
|
||||
name = route.name.format(basename=basename)
|
||||
ret.append(url(regex, view, name=name))
|
||||
|
|
Loading…
Reference in New Issue
Block a user