mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 16:40:03 +03:00
Fix route iteration in get_urls
This commit is contained in:
parent
64dcb23852
commit
2096cc97dd
|
@ -251,44 +251,42 @@ class SimpleRouter(BaseRouter):
|
||||||
lookup = self.get_lookup_regex(viewset)
|
lookup = self.get_lookup_regex(viewset)
|
||||||
routes = self.get_routes(viewset)
|
routes = self.get_routes(viewset)
|
||||||
|
|
||||||
|
is_viewset = isclass(viewset) and issubclass(viewset, ViewSetMixin)
|
||||||
|
|
||||||
for route in routes:
|
for route in routes:
|
||||||
|
|
||||||
if isinstance(viewset, View):
|
if is_viewset:
|
||||||
# `viewset` is a Django CBV. REST Frameworks `ViewSet`s
|
|
||||||
# are included in this if-statement because `ViewSet`s
|
|
||||||
# subclass `APIView`, which subclasses `View`.
|
|
||||||
|
|
||||||
# Only actions which actually exist on the viewset will be bound
|
# Only actions which actually exist on the viewset will be bound
|
||||||
mapping = self.get_method_map(viewset, route.mapping)
|
mapping = self.get_method_map(viewset, route.mapping)
|
||||||
if not mapping:
|
if not mapping:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Build the url pattern
|
||||||
|
regex = route.url.format(
|
||||||
|
prefix=prefix,
|
||||||
|
lookup=lookup,
|
||||||
|
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:]
|
||||||
|
|
||||||
|
initkwargs = route.initkwargs.copy()
|
||||||
|
initkwargs.update({
|
||||||
|
'basename': basename,
|
||||||
|
'detail': route.detail,
|
||||||
|
})
|
||||||
|
|
||||||
view = viewset.as_view(mapping, **initkwargs)
|
view = viewset.as_view(mapping, **initkwargs)
|
||||||
|
name = route.name.format(basename=basename)
|
||||||
|
django_path = re_path(regex, view, name=name)
|
||||||
else:
|
else:
|
||||||
# assume that `viewset` is a Django view
|
# assume that `viewset` is a Django view
|
||||||
view = viewset
|
django_path = path(prefix, viewset, name=prefix)
|
||||||
|
|
||||||
# Build the url pattern
|
|
||||||
regex = route.url.format(
|
|
||||||
prefix=prefix,
|
|
||||||
lookup=lookup,
|
|
||||||
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:]
|
|
||||||
|
|
||||||
initkwargs = route.initkwargs.copy()
|
|
||||||
initkwargs.update({
|
|
||||||
'basename': basename,
|
|
||||||
'detail': route.detail,
|
|
||||||
})
|
|
||||||
|
|
||||||
name = route.name.format(basename=basename)
|
|
||||||
django_path = re_path(regex, view, name=name)
|
|
||||||
|
|
||||||
ret.append(django_path)
|
ret.append(django_path)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user