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

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

问题描述

有没有一种简单的方法可以做到以下几点?

Is there an easy way to do the following?

在视图中得到请求后,向用户发送一个文件,以及一个重新渲染的页面模板?类似于合并"响应与文件和render_to_response

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)

问题是:我如何将它们合并"在一起?有没有一种简单的方法可以使用 django 标准功能来做到这一点?我必须为此使用 Ajax 吗?(我不熟悉 ajax ......所以如果有办法做到这一点,最好是)

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)

谢谢,

推荐答案

如果您简化了对超文本传输​​协议的看法,那么您的用户与您的应用程序交互的方式就是发送请求,您的应用程序为其提供响应.在 4.2 of RFC 6266 部分中,当您返回匹配的响应时附件"处置类型,您指示用户的客户端它应该提示用户在本地保存响应,而不是正常处理它".

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

不支持提供包含 HTML 和其他一些内容类型的响应,并且可以指示客户端保存一个并显示另一个.它不受支持的事实并不意味着您不应该探索提供此类用户体验的替代方案,但它应该暗示我们没有实践它并且用户不会期望它.

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.

但是,抛开事实不谈,如果您真的想这样做,您需要了解提供文件和显示新的 HTML 文档将需要两个单独的请求/响应上下文.最简单的设计将包括用户将要发起检索文件请求的页面也订阅应用程序,以便在用户完成检索文件时收到通知,然后显示一些新内容或重定向到一个新页面.

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天全站免登陆