【发布时间】: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