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

后端 未结 5 2053
死守一世寂寞
死守一世寂寞 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 02:57

    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.

提交回复
热议问题