-1

I am using Flask in python with HTML. I do not receive anything from the form, I do not know what I have wrong, I leave my code below. I get "none" from the form when I execute the request.form.get("url")

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" name=“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>

PYTHON FILE

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)

I think the problem is in the HTML form but I don't know. Any help is helpful, thanks.

2 Answers2

1

To your input-field you have to add a name attribute

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

should do the job. When sent to the flask-backend request.form.get() does not look for the id but the name attribute.

Lenntror
  • 186
  • 10
1

You got None because you didn't specify the name attribute in your url field, like this:

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

request.form.get() looks at the value of name attribute and not the id

Tobin
  • 2,029
  • 2
  • 11
  • 19