Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 821
Ryan Allen Avatar asked Sep 11 '17 20:09

Ryan Allen


1 Answers

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.

like image 170
Ryan Allen Avatar answered Sep 22 '22 17:09

Ryan Allen