Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

python - Django HttpResponse - response with file and content

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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...