Django:如何返回原始响应
这就是我想要的。
- 向外部站点提交POST请求(即登录信息)。
- 接收响应
- 将原始响应返回到我的客户端浏览器(包含用于登录的 cookie
验证)。 - 如果客户尝试在新选项卡中访问该网站,他会发现他已经登录。
我成功完成了步骤 1 和 2。 2(提交 POST 并收到站点的响应)。
request = urllib2.Request(url, formData, headers)
response = urllib2.urlopen(request)
但是当我尝试在视图中返回它时,
return response
我收到以下错误
Django Version: 1.3.1
Exception Type: AttributeError
Exception Value: addinfourl instance has no attribute 'has_header'
Exception Location:D:\Python27\lib\site-packages\django\utils\cache.py in patch_vary_headers
注释: 我以前遇到过 csrf 错误,但我使用装饰器 @csrf_exempt & 禁用了 csrf错误消失了
This is what I want.
- Submit a POST request to a external site(i.e login information).
- Receive the response
- Return the raw response to my client's browser(containing the cookies for login
validation). - If the client tries to access the site in new tab he finds that he is already signed in.
I successfully completed steps 1 & 2 (submitted POST & received the response from the site).
request = urllib2.Request(url, formData, headers)
response = urllib2.urlopen(request)
But when I try to return it in the view
return response
I get the folllowing error
Django Version: 1.3.1
Exception Type: AttributeError
Exception Value: addinfourl instance has no attribute 'has_header'
Exception Location:D:\Python27\lib\site-packages\django\utils\cache.py in patch_vary_headers
note:
I had a csrf error previosly,but i disabled csrf using decorator @csrf_exempt & the error was gone
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应直接从 urlopen 方法返回响应。相反,您的视图应该返回 django 的 HttpResponse,其中正文和标头应设置为原始响应中的内容:
You shouldn't return the response from urlopen method directly. Instead your view should return an instance of django's HttpResponse, where body and the headers should be set to those from the original response:
首先,在视图中提交 URL 请求作为响应是没有意义的。但是,即使你可以,那也不会达到你想要的效果。没有办法让用户登录到不同的站点,原因很明显:这将是一个巨大的安全漏洞。
Firstly, it doesn't make sense to submit a URL request as the response in a view. But, even if you could, that wouldn't do what you want. There's no way to log a user in to a different site, for what should be obvious reasons: that would be a massive security hole.