【问题标题】:I get "none" from the form when I execute the request.form.get("url")当我执行 request.form.get(\"url\") 时,我从表单中得到 \"none\"
【发布时间】:2022-08-17 19:34:07
【问题描述】:

我在带有 HTML 的 python 中使用 Flask。 我没有从表格中收到任何东西,我不知道我有什么问题,我将我的代码留在下面。 当我执行 request.form.get(\"url\") 时,我从表单中得到 \"none\"

HTML

<!DOCTYPE html>
<html lang=\"en\">

<head>
    <meta charset=\"UTF-8\">
    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
    <title>YouTube Download</title>
    <link rel=\"stylesheet\" href=\"../static/css/style.css\">
    <link href=\"{{ url_for(\'static\', filename=\'css/style.css\') }}\" rel=\"stylesheet\">
    <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css\">
</head>

<body>
    <div class=\"banner\">
        <h1 class=\"title\" data-text=\"Youtube Download\"><i class=\"fa fa-music\"></i> Youtube Download</h1>
        <form action=\"{{ url_for(\'downloadMP3\') }}\" method=\"POST\">
            <p>Ingresa la URL del Video
            </p>
            <br>
            <input class=\"form-control\" type=\"text\" id=\"url\" >
            <div class=\"btn-group\">
                <button class=\"btn\" type=\"submit\" formaction=\"/downloadMP4\">
                    <i class=\"fa fa-download\"> </i> Download MP4
                </button>
                <button class=\"btn\" type=\"submit\" formaction=\"/downloadMP3\">
                    <i class=\"fa fa-download\"> </i> Download MP3
                </button>
            </div>
        </form>
    </div>
</body>


</html>

蟒蛇文件

from flask import Flask, render_template, request, Response, redirect, send_file
import os
from os import remove
import pafy
import moviepy.editor as mp

app = Flask(__name__)
path=os.getcwd() + \'/\'

@app.route(\'/\')
def route():
    return render_template(\'index.html\')

@app.route(\'/downloadMP4\', methods=[\'GET\', \'POST\'])
def downloadMP4():
    if request.method == \'POST\':
        url = request.form.get(\"url\")
        video = pafy.new(url)
        best = video.getbest(preftype=\"mp4\")
        best.download(path)
        p = path + video.title + \'.mp4\'

    return send_file(p, as_attachment=True)

@app.route(\'/downloadMP3\', methods=[\'GET\', \'POST\'])
def downloadMP3():
    if request.method == \'POST\':
        url = request.form.get(\"url\")
        video = pafy.new(url)
        best = video.getbest(preftype=\"mp4\")
        best.download(path)
        name = path + video.title + \'.mp4\'
        clip = mp.VideoClip(name)
        clip.audio.write_audiofile(path + video.title + \'.mp3\')
        p = path + video.title + \'.mp3\'

    return send_file(p, as_attachment=True)

if __name__ == \'__main__\':
    app.run(host=\'localhost\', port=5000)

我认为问题出在 HTML 表单中,但我不知道。 任何帮助都是有帮助的,谢谢。

    标签: python forms flask request


    【解决方案1】:

    你得到了None,因为你没有在url field 中指定name attribute,像这样:

    <input class="form-control" type="text" id="url" name="url">
    

    request.form.get() 查看 name attribute 的值,而不是 id

    【讨论】:

      【解决方案2】:

      对于您的输入字段,您必须添加一个名称属性

      <input class="form-control" type="text" id="url" name="url">
      

      应该做的工作。当发送到烧瓶后端时,request.form.get() 不查找 id 而是查找 name 属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-31
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多