如何在Django Rest Framework SimpleRouter上使斜杠为可选 [英] How can I make a trailing slash optional on a Django Rest Framework SimpleRouter

查看:243
本文介绍了如何在Django Rest Framework SimpleRouter上使斜杠为可选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说,您可以设置 trailing_slash = False ,但是如何允许两个端点在有斜线或无斜线的情况下工作?

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

推荐答案

您可以覆盖 < SimpleRouter类的code> __ init __ 方法

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.

这篇关于如何在Django Rest Framework SimpleRouter上使斜杠为可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆