Django 的 HttpResponseRedirect 似乎剥离了我的子域?
每当我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Django 有一些方法总是应用于响应。其中之一是 django.http.utils.fix_location_header 。这可确保重定向响应始终包含绝对 URI(按照 HTTP 规范的要求)。
此方法使用
request.build_absolute_uri
,而后者又使用request.get_host
。get_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 usesrequest.get_host
.get_host
tries to get theHTTP_HOST
fromrequest.META
, falling back to usingSERVER_NAME
.My guess is that your server isn't providing the
HTTP_HOST
and that yourSERVER_NAME
is set tomydomain.com
.Hopefully now you know what you're looking for, you can run some tests to see what's going wrong.
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