Django的HttpResponseRedirect似乎剥离了我的子域? [英] Django's HttpResponseRedirect seems to strip off my subdomain?

查看:153
本文介绍了Django的HttpResponseRedirect似乎剥离了我的子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我的django网站在视图对象中调用HttpResponseRedirect来重定向到另一个url,它将剥离子域并返回主站点。我正在处理Django的SVN分支。以下是示例:

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?

推荐答案

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

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).

此方法使用 request.build_absolute_uri ,它反过来使用 request.get_host get_host 尝试从 request.META 中获取 HTTP_HOST 回到使用 SERVER_NAME

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.

我的猜测是,你的服务器没有提供 HTTP_HOST ,您的 SERVER_NAME 设置为 mydomain.com

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.

这篇关于Django的HttpResponseRedirect似乎剥离了我的子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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