31

The docs say you can set trailing_slash=False but how can you allow both endpoints to work, with or without a trailing slash?

Ryan Allen
  • 4,097
  • 3
  • 22
  • 29

5 Answers5

39

You can override the __init__ method of the SimpleRouter class:

from rest_framework.routers import SimpleRouter


class OptionalSlashRouter(SimpleRouter):

    def __init__(self):
        super().__init__()
        self.trailing_slash = '/?'

The ? character will make the slash optional for all available routes.

Cesar Canassa
  • 13,705
  • 6
  • 56
  • 65
Ryan Allen
  • 4,097
  • 3
  • 22
  • 29
  • Funny I was thinking about this today. But I'm making use of url Router. Will try that and see what happens. +1 – Bernard 'Beta Berlin' Parah Sep 11 '17 at 20:34
  • 11
    I use `DefaultRouter` and had to do this: `class OptionalSlashRouter(DefaultRouter): def __init__(self, *args, **kwargs): super(DefaultRouter, self).__init__(*args, **kwargs) self.trailing_slash = '/?' ` – Def_Os Apr 02 '18 at 23:35
  • 2
    You can pass `/?` [to](https://github.com/encode/django-rest-framework/blob/3.9.1/rest_framework/routers.py#L168) the [constructor](https://www.django-rest-framework.org/api-guide/routers/#simplerouter). – x-yuri Feb 21 '19 at 16:36
  • 3
    The __init__ method [overrides `trailing_slash` to always be `/`](https://github.com/encode/django-rest-framework/blob/453196e9c3a581bac3bf68eb8c9cdd7d28d2dcd6/rest_framework/routers.py#L169) when truthy. – Ryan Allen Feb 21 '19 at 16:48
  • 4
    Put the super init method call before `self.trailing_slash = '/?'`. – Milso Mar 13 '20 at 10:58
24

You can also override this setting by passing a trailing_slash argument to the SimpleRouter constructor as follows:

from rest_framework import routers

router = routers.SimpleRouter(trailing_slash=False)
dill
  • 257
  • 2
  • 3
  • 5
    Then the URL with a trailing slash doesn't work though – Ryan Allen Feb 16 '18 at 14:59
  • That's the point, but in such case you will probably have to configure to support both at the http server level. Example: Apache has the mod_rewrite module and you can do a simple redirect to urls without trailing slash – OzzyTheGiant Apr 16 '19 at 14:53
  • If I have `trailing_slash=False`, and I write the URLs to register with a slash at the end, and have APPEND_SLASH not set to False, then both the URLs with and without the trailing slash work fine, as the URL without a trailing slash redirects to the one with a trailing slash. – mic Oct 13 '20 at 18:58
  • The issue is, URLs for subpages from DRF such as `my_model/1/` become `my_model//1`. So it seems better to keep the default of `trailing_slash=True`, register the URL without a trailing slash, and have APPEND_SLASH=True. – mic Oct 13 '20 at 19:12
6

If you're using DRF's routers and viewsets, you can include /? in your prefix.

from rest_framework import routers

from .views import ClientViewSet

router = routers.SimpleRouter(trailing_slash=False)
router.register(r"clients/?", ClientViewSet)
Noel Llevares
  • 11,823
  • 2
  • 41
  • 72
1

For anyone using rest_framework_extensions with ExtendedSimpleRouter, the accepted solution needs a small modification. The self.trailling_slash has to be after the super() like this.

from rest_framework_extensions.routers import ExtendedSimpleRouter

class OptionalSlashRouter(ExtendedSimpleRouter):
    def __init__(self):
        super(ExtendedSimpleRouter, self).__init__()
        self.trailing_slash = "/?"
Cedric Holz
  • 51
  • 1
  • 4
1

I found the easiest way to do this is just to set up your URLs individually to handle the optional trailing slash, e.g.

from django.urls import re_path

urlpatterns = [
    re_path('api/end-point/?$', api.endPointView),
    ...

Not a DRY solution, but then it's only an extra two characters for each URL. And it doesn't involve overriding the router.

gornvix
  • 2,596
  • 2
  • 25
  • 57