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

后端 未结 5 1983
死守一世寂寞
死守一世寂寞 2021-02-05 02:43

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

5条回答
  •  后悔当初
    2021-02-05 03:11

    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 = "/?"
    

提交回复
热议问题