简体   繁体   English

从 HTML 段落请求数据以使用 Python Flask 下载

[英]Request data from HTML paragraph to download using Python Flask

I know that <p> is a display element.我知道<p>是一个显示元素。 Therefore, I am displaying data inside the paragraph as a python variable in the <body>因此,我将段落内的数据显示为<body>的 python 变量

 <p>
        {{ filename }}
 </p>

But In the <header> I have a download button that downloads whatever in the <p> as a text file.但是在<header>我有一个下载按钮,可以将<p>中的任何内容下载为文本文件。

To do that I stored another version of the data in a hidden <input> Then I put a download button as follows:为此,我将另一个版本的数据存储在隐藏的<input>然后我放置了一个下载按钮,如下所示:

<input method="post" type="hidden" name="fileData" value="{{ filename }}" />

<a  href="{{ url_for('download_file') }}">Download</a>

In Flask I have this:在 Flask 中,我有这个:

@app.route('/database_download', methods=['GET', 'POST'])
def download_file():
    if request.method == 'POST':
        f = request['fileData']
        response = make_response(f)
        response.headers["Content-Disposition"] = "attachment; filename=result.txt"
        return response
    return render_template('upload.html')

The problem I have is that when I remove return render_template from the end, it gives me a ValueError: View function did not return a response or if I keep it then when I click on download nothing happens, it just re-direct me to the same page.我遇到的问题是,当我从最后删除return render_template时,它给了我一个ValueError: View function did not return a response或者如果我保留它然后当我点击下载时什么也没有发生,它只是将我重定向到同一页。 Any suggestions please ?请问有什么建议吗?

Your requests must be GETs, because you're not passing the您的请求必须是 GET,因为您没有通过

if request.method == 'POST':

line, and so getting the responses you described.行,并因此获得您所描述的响应。

Get out your debugger, and figure out what the value of request.method is, and then go from there.拿出你的调试器,找出 request.method 的值是什么,然后从那里开始。

Or, you can double check that you're sending POST requests in your client-side code.或者,您可以仔细检查您是否在客户端代码中发送 POST 请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM