【问题标题】:Django : How to change redirect destination domain of HttpResponseRedirectDjango:如何更改 HttpResponseRedirect 的重定向目标域
【发布时间】:2011-08-06 21:21:20
【问题描述】:

另一个网站将用户重定向到我的网站。 Django 在我的服务器上托管两个域

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

根据引用站点传递的“请求”中传递的凭据,我知道在哪里重定向到用户。但是我有一个限制,我需要每次都使用具有httpredirectresponse(reverse('DemoVar_response')) 的特定视图方法。我的代码看起来像这样

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

如果来自内部链接的调用,HttpResponseRedirect(reverse('DemoVar_response')) 会指向请求的来源域,但由于我收到来自不同网站的重定向请求,HttpResponseRedirect 会退回到默认站点。

如何使 HttpPresponseRedirect 转到相应的域?我在重定向时有目标域信息,但我应该在哪里设置呢?

【问题讨论】:

    标签: django apache redirect


    【解决方案1】:

    HttpResponseRedirect 只接受一个 URL。而reverse 只是返回一个路径,即没有域的 URL。

    所以你可以轻松做到:

    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 只是将 URL 的两个元素连接在一起,确保不重复斜杠等。

    【讨论】:

    • 它的 urlparse.urljoin 而不是 urlparse.join。除此之外,您的解决方案效果很好。
    猜你喜欢
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 2014-09-13
    • 2021-02-24
    相关资源
    最近更新 更多