How can I make a trailing slash optional on a Django Rest Framework SimpleRouter

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.


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)