Django's HttpResponseRedirect seems to strip off my subdomain?

旧城冷巷雨未停 提交于 2019-11-27 17:03:36

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1457006/djangos-httpresponseredirect-seems-to-strip-off-my-subdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!