Return loaded and displayed page in one checkbox

I want to return the displayed page and the uploaded file as a response to the request. I tried to return a tuple of both answers, but it does not work. How can I download and page?

return response, render_template('database.html') return render_template('database.html'), response 

Can Flask work with such a scenario? Seems like a normal problem, I just want to send the file back for download, and then display the page.

+5
source share
1 answer

You cannot return multiple responses to a single request. Instead, create and save files somewhere, and serve them using a different route. Return the processed template with the URL for the route that the file will serve.

 @app.route('/database') def database(): # generate some file name # save the file in the `database_reports` folder used below return render_template('database.html', filename=stored_file_name) @app.route('/database_download/<filename>') def database_download(filename): return send_from_directory('database_reports', filename) 

In the template, use url_for to create the download url.

 <a href="{{ url_for('database_download', filename=filename) }}">Download</a> 
+5
source

Source: https://habr.com/ru/post/1237141/


All Articles