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