Django 的 HttpResponseRedirect 似乎剥离了我的子域?

发布于 2024-08-06 05:34:03 字数 360 浏览 3 评论 0原文

每当我的 django 站点在视图对象中调用“HttpResponseRedirect”以重定向到另一个 url 时,它就会剥离子域并返回到主站点。我正在 Django 的 SVN 分支工作。这是示例:

#Request comes in as https://sub1.mydomain.com
def view(request):
  return HttpResponseRedirect("/test_url") #The browser will actually get redirected to https://mydomain.com/test_url


这样做有什么原因吗?我应该重定向到包括子域的完整路径吗?

Whenever my django site calls "HttpResponseRedirect" in a view object to redirect to another url it strips off the sub-domain and goes back to the main site. I'm working off of the SVN branch of Django. Here is the example:

#Request comes in as https://sub1.mydomain.com
def view(request):
  return HttpResponseRedirect("/test_url") #The browser will actually get redirected to https://mydomain.com/test_url

Is there a reason this is done? Should I have to redirect to the full path including the sub-domain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独 2024-08-13 05:34:03

Django 有一些方法总是应用于响应。其中之一是 django.http.utils.fix_location_header 。这可确保重定向响应始终包含绝对 URI(按照 HTTP 规范的要求)。

此方法使用 request.build_absolute_uri,而后者又使用 request.get_hostget_host 尝试从 request.META 获取 HTTP_HOST,并回退到使用 SERVER_NAME

我的猜测是您的服务器未提供 HTTP_HOST 并且您的 SERVER_NAME 设置为 mydomain.com

希望现在您知道自己在寻找什么,可以运行一些测试来看看出了什么问题。

Django has some methods it always applies to a response. One of these is django.http.utils.fix_location_header. This ensures that a redirection response always contains an absolute URI (as required by HTTP spec).

This method uses request.build_absolute_uri, which in-turn uses request.get_host. get_host tries to get the HTTP_HOST from request.META, falling back to using SERVER_NAME.

My guess is that your server isn't providing the HTTP_HOST and that your SERVER_NAME is set to mydomain.com.

Hopefully now you know what you're looking for, you can run some tests to see what's going wrong.

≈。彩虹 2024-08-13 05:34:03

HttpResponseRedirect 将仅返回带有 Location 标头集的 302 状态代码。 url 解析器不会考虑子域(请参阅 http://code.djangoproject.com/ticket /8896)。您最好的选择是从头开始重建它(META 上的 HTTP_HOST)或仅使用 http://thingsilearned.com/2009/01/05/using-subdomains-in-django/

干杯

The HttpResponseRedirect will simply return a 302 status code with the Location header set. The url resolver won't take the subdomain into consideration ( see http://code.djangoproject.com/ticket/8896 ). Your best bet it to either reconstruct it from scratch (HTTP_HOST on META) or just use the Middleware from http://thingsilearned.com/2009/01/05/using-subdomains-in-django/ .

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文