-1

I'm trying to get a simple web form up and running that only asks for a URL.

This is the HTML Code (index.html)

<!DOCTYPE html>
<html>
    <body>
        <form name = 'test' action = "." method = "post">
            <form action="test.php" method="get">
                URL <input type="text" link="link" name = "URL"/>
                <input type="submit" />
        </form>
    </body>
</html>

I'm using Flask to run the simple web application this is the Flask Code: (app.py)

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")

def index():
    return render_template('index.html')

@app.route("/", methods = ["POST"])

def get_value():
    url = request.form["URL"]
    return 'The url is ' + url

if __name__ == "__main__":
    app.run(debug=True)

and I'm trying to get the inputted URL to another python script so I can do something with it, this is the other python script: (url.py)

from app import get_value

print(get_value())

However, whenever I run python3 url.py it gives me this error:

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

Any idea how to print get the URL over successfully? In a lot of detail preferably because I am very new to Flask.

1 Answers1

1

The error occurs because you called a function that needs data from a request to get the user inputs. You should call the url handling function instead letting the handling function call the retrieval of the url.

Consider this answer https://stackoverflow.com/a/11566296/5368402 to make sure you pass the url correctly. Now that you have your url, simply pass it to your other script.

import url # your url.py module

@app.route("/", methods = ["POST"])

def get_value():
    input_url = request.form["URL"]
    url.handle_url(input_url) #call a function inside url.py
    
e.Fro
  • 387
  • 1
  • 14