开发者

Django : How to change redirect destination domain of HttpResponseRedirect

A diffe开发者_运维百科rent site redirects users to my site. Django host two domains on my server

 1. domain1.com
 2. domain2.com -> domain1.com/domain2 ( using ProxyPass ReverseProxyPass in apache)

Based on credentials passed in the 'request' passed by referring site, I know where to redirect to the user. But I have a constraint that I need to use a particular view method having httpredirectresponse(reverse('DemoVar_response')) every time. My code looks something like this

app/views.py

 return HttpResponseRedirect(reverse('DemoVar_response',args=['Successful']))

app/urls.py

 url(r'^response/(?P<response>[\s\w\d-]+)/$','response', name='DemoVar_response')

In case of call from internal link, HttpResponseRedirect(reverse('DemoVar_response')) leads to the domain of origin of request, but since I get a redirect request from different website, HttpResponseRedirect falls back to default site.

How can I make HttpPresponseRedirect go to the appropriate domain? I have the destination domain info at the time of redirect, but where should I set it?


HttpResponseRedirect simply takes a URL. And reverse simply returns a path, ie a URL without the domain.

So you can easily do:

import urlparse
domain = request.GET['domain'] # or however you are getting it
destination = reverse('DemoVar_response',args=['Successful'])
full_address = urlparse.urljoin(domain, destination)
return HttpResponseRedirect(full_address)

urlparse.urljoin simply joins the two elements of the URL together, ensuring that slashes aren't duplicated and so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜