问题描述
在我的应用程序中,我需要显示REST网址(末尾没有斜杠).我试过下面的组合,但没有用.
In my application , I need to display the REST url(s) without a slash at the end. I have tried below combination but it didnt work.
在settings.py中添加了 APPEND_SLASH = True
.
Added APPEND_SLASH=True
in the settings.py
以及urls.py文件
and on the urls.py file
from rest_framework.routers import SimpleRouter
router = SimpleRouter(trailing_slash=False)
当我在邮递员的末尾使用不带斜杠的url进行添加后,它给了我一个404错误-URL找不到.但是最后用斜杠可以正常工作.
After adding this when I am calling the urls without slash at the end in the postman, it is giving me an 404 error- URL not found. But with slash at the end is working fine.
是否有任何选项可以使此url末尾不带斜杠?尤其是帖子网址
Is there any option to make this url without with slash at the end ? Especially for the post urls
推荐答案
APPEND_SLASH 会将其附加到请求中(例如,mysite/blog-> mysite/blog/).这不是您想要的,因为您的urlconf明确表示不应使用斜杠.
APPEND_SLASH will append it to the request (e.g. mysite/blog --> mysite/blog/). This is not what you want, since your urlconf explicitly says there should be no slash.
默认情况下,APPEND_SLASH也为True.因此,您需要将其设置为False.这样,如果您在不加斜杠的情况下发出请求,则Django不会自动添加斜杠.
Also APPEND_SLASH is True by default. So you need to set it to False instead. That way, if you make a request without a slash, Django won't automatically add in a slash.
这篇关于DRF发布网址,不带斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!