没有尾部斜杠的 django url 不会重定向
我有两个应用程序位于两台不同的计算机上。在计算机 A 上,在 urls.py
文件中,我有一行如下所示的行:
(r'^cast/$', 'mySite.simulate.views.cast')
并且该 url 适用于 mySite.com/cast/
和 mySite .com/cast
。但在计算机 BI 上,有一个类似的 url 写法如下:
(r'^login/$', 'mySite.myUser.views.login')
出于某种原因,在计算机 B 上,url mySite.com/login
/ 可以工作,但 mySite.com/login
将挂起并且不会像在计算机 A 上那样直接返回 mySite.com/login/
。我是否错过了什么?在我看来,两个 url.py 文件看起来都一样。
I've got two applications located on two separate computers. On computer A, in the urls.py
file I have a line like the following:
(r'^cast/
And that url will work for both mySite.com/cast/
and mySite.com/cast
. But on computer B I have a similar url written out like:
(r'^login/
For some reason on computer B the url mySite.com/login
/ will work but mySite.com/login
will hang and won't direct back to mySite.com/login/
like it will on computer A. Is there something I missed? Both url.py
files look identical to me.
, 'mySite.simulate.views.cast')
And that url will work for both mySite.com/cast/
and mySite.com/cast
. But on computer B I have a similar url written out like:
For some reason on computer B the url mySite.com/login
/ will work but mySite.com/login
will hang and won't direct back to mySite.com/login/
like it will on computer A. Is there something I missed? Both url.py
files look identical to me.
, 'mySite.myUser.views.login')
For some reason on computer B the url mySite.com/login
/ will work but mySite.com/login
will hang and won't direct back to mySite.com/login/
like it will on computer A. Is there something I missed? Both url.py
files look identical to me.
And that url will work for both mySite.com/cast/
and mySite.com/cast
. But on computer B I have a similar url written out like:
For some reason on computer B the url mySite.com/login
/ will work but mySite.com/login
will hang and won't direct back to mySite.com/login/
like it will on computer A. Is there something I missed? Both url.py
files look identical to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
或者您可以这样编写您的网址:
尾部斜杠后面的问号使其在正则表达式中是可选的。如果由于某些原因您不想使用 APPEND_SLASH 设置,请使用它。
Or you can write your urls like this:
The question sign after the trailing slash makes it optional in regexp. Use it if for some reasons you don't want to use APPEND_SLASH setting.
检查 settings.py 文件中的 APPEND_SLASH 设置
django 文档中的更多信息
check your
APPEND_SLASH
setting in the settings.py filemore info in the django docs
这改进了@Michael Gendin 的答案。他的回答提供了具有两个不同 URL 的相同页面。最好让
login
自动重定向到login/
,然后将后者作为主页:This improves on @Michael Gendin's answer. His answer serves the identical page with two separate URLs. It would be better to have
login
automatically redirect tologin/
, and then serve the latter as the main page:我也遇到过同样的问题。我的解决方案是在正则表达式的结束行之前放置一个(|/)。
url(r'^artists/(?P[\d]+)(|/)$', ArtistDetailView.as_view()),
I've had the same problem too. My solution was put an (|/) before the end line of my regular expression.
url(r'^artists/(?P[\d]+)(|/)$', ArtistDetailView.as_view()),
附加斜杠不重定向,在设置中使用它代替 CommonMiddleware,Django 2.1:
添加到您的主应用程序目录middleware.py:
Append slash without redirect, use it instead of CommonMiddleware in settings, Django 2.1:
Add to your main app directory middleware.py:
在某些情况下,当我们的某些用户以不同的结尾调用 API 时,我们会遇到问题。通常,我们的用户使用 Postman 来实现这一点,并且不担心端点处的斜线。结果,我们收到了支持问题请求,因为用户忘记在 POST 请求末尾附加斜杠
/
。我们通过使用适用于 Django 3.2+ 和 Django 4.0+ 的自定义中间件解决了这个问题。之后,Django 可以处理任何带斜杠或不带斜杠的 API 的 POST/PUT/DELETE 请求。使用此中间件不需要更改settings.py中的
APPEND_SLASH
属性因此,在
settings.py
中需要删除当前的'django.middleware.common.CommonMiddleware'
并插入新的中间件。确保在下面的示例中将your_project_name
更改为您的真实项目名称。添加到您的主应用程序目录 middleware.py:
这个答案可能看起来类似于 马克斯·特卡琴科回答。但他的代码在最新版本的 Django 中对我不起作用。
In some cases, we have issues when some of our users call API with different endings. Usually, our users use Postman for that and are not worried about slash at the endpoint. As result, we receive issue requests in support, because users forgot to append a slash
/
at the end of POST requests.We solved it by using a custom middleware that works for us in Django 3.2+ and Django 4.0+. After that, Django may handle any POST/PUT/DELETE requests to your API with slash or without them. With this middleware unneeded to change
APPEND_SLASH
property in settings.pySo, in the
settings.py
need to remove your current'django.middleware.common.CommonMiddleware'
and insert new middleware. Make sure, you changeyour_project_name
in my example below on your real project name.Add to your main app directory middleware.py:
This answer may look similar to Max Tkachenko answer. But his code didn't work for me in the latest versions of Django.
我也遇到过同样的问题。就我而言,它是 urls.py 中旧版本的旧版本,来自 staticfiles:
MEDIA_URL 为空,因此该模式与所有内容匹配。
I've had the same problem. In my case it was a stale leftover from some old version in urls.py, from before staticfiles:
MEDIA_URL was empty, so this pattern matched everything.
在 Django 项目的设置文件 (settings.py) 中,确保 APPEND_SLASH 设置为 True:
接下来,将 CommonMiddleware 添加到 MIDDLEWARE 设置:
通过此配置,Django 的 CommonMiddleware 将自动处理 URL 重定向并添加或删除尾部斜杠根据需要。这意味着 Django REST Framework 将正确处理对“xyz/”和“xyz”的请求。
注意 - 发出请求时,通常建议包含尾部斜杠以符合 RESTful 约定。但是,通过上述配置,不带尾部斜杠的请求仍将按预期工作。
In your Django project's settings file (settings.py), make sure the APPEND_SLASH setting is set to True:
Next, add the CommonMiddleware to the MIDDLEWARE setting:
With this configuration, Django's CommonMiddleware will automatically handle URL redirection and add or remove the trailing slash as needed. This means that requests to both 'xyz/' and 'xyz' will be correctly processed by Django REST Framework.
Note - When making requests, it is generally recommended to include the trailing slash to conform to RESTful conventions. However, with the above configuration, requests without the trailing slash will still work as expected.
您可以使用此代码段
,然后将您的
path
替换为tr_path
You can use this snippet
then replace your
path
totr_path