Django HttpResponse - 文件和内容的响应 [英] Django HttpResponse - response with file and content

查看:962
本文介绍了Django HttpResponse - 文件和内容的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在视图中获取请求后,
向用户发送一个文件,再加上一个重新渲染页面的模板?
像文件和render_to_response一样的合并响应

这是我如何返回一个文件的回复:


$

$ b content_type = mimetypes.guess_type(filename)
$ [0]
response = HttpResponse(wrapper,content_type = content_type)
response ['Content-Length'] = os.path.getsize(filename)
response ['Content-Disposition'] =attachment; filename =%s%filename
返回响应

返回带有渲染数据的普通模板:

  data = getData()
return render_to_response('../ templates / some_template.html',{'data':data,})

(我可能已经删除了一些重要的线条与复制粘贴,但为了使我的观点 - 这段代码工作,问题不是与这两个代码示例)



问题是:我如何合并他们都来了GETHER?
有一个简单的方法来做到这一点与django标准功能?
我必须使用Ajax吗? (我不熟悉ajax ...所以如果有办法做到这一点没有,最好是)



谢谢,

解决方案

如果您简化了超文本传输​​协议的视图,则用户与应用程序交互的方式是发送应用程序提供响应的请求。在 RFC 6266 4.2节,当您返回与附件匹配的响应时配置类型,您指示用户的客户端应该提示用户在本地保存响应,而不是正常处理。



不支持包含HTML和其他内容类型的响应,可以指示客户端保存并显示另一个。不支持这一点的事实并不意味着您不应该探索提供这种用户体验的替代方案,但它应该暗示我们不会练习,用户不会期待。



但事实如此,如果你真的想这样做,你需要明白,服务文件和显示一个新的HTML文档将需要两个单独的请求/响应上下文。最简单的设计将包括用户将要从哪个页面启动检索文件的页面,并在用户已完成检索文件时通知应用程序,然后关闭显示一些新内容或重定向到一个新的页面。


Is there an easy way to do the following?

after getting the request in the view, send back to the user a file, plus a re-rendered template of the page? something like "merging" reponse with a file and render_to_response

This is how I return a response with a file:

filename = "/path/to/somewhere"
wrapper  = FileWrapper(open(filename))
content_type = mimetypes.guess_type(filename)[0]
response = HttpResponse(wrapper,content_type=content_type)
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = "attachment; filename=%s"%filename
return response

This is how I return an ordinary template with rendered data:

data = getData()
return render_to_response('../templates/some_template.html', {'data': data,})

(I might have dropped some important lines with copy-paste, but to make my point - this code works, the problem is not with these two code samples)

the question is: how do I "merge" both of them together? is there a simple way to do this with django standard functionality? Do I have to use Ajax for this? (I'm not familiar with ajax... so if there's a way to do this without, it's preferable)

Thanks,

解决方案

If you simplify your view of the Hypertext Transfer Protocol, the way your users interact with your application is by sending a request for which your application provides a response. In section 4.2 of RFC 6266, when you return a response that matches the "attachment" disposition type, your instructing the user's client that it should "prompt the user to save the response locally, rather than process it normally".

There's no support for serving a response that contains HTML and some other content type and which can instruct the client to save one and display the other. The fact that it isn't supported doesn't mean that you shouldn't explore for alternatives to provide such a user experience, but it should hint that we're not practicing it and the user wouldn't expect it.

But, facts aside, if you really want to do it you need to understand that serving the file and displaying a new HTML document is going to take two separate request/response contexts. The simplest design would include that page from which the user is going to initiate the request to retrieve the file also subscribes with the application to be notified when the user has retrieved the file in completion and then goes off to display some new content or redirect to a new page.

这篇关于Django HttpResponse - 文件和内容的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆