Reputation: 1
Very new to python/flask and actually to programming in general.
I'm trying to create a flask html form that will pull data from an API and take the user input and run it through a separate python script file named cityid.py. Then have it return the results of the cityid.py script back into the same HTML page, but below the input box.
End result would be something like this on the HTML page:
Enter your city: ______________ (submit button)
results printed here from the cityid.py script
Here's the form I have in my HTML file.
<form name="search" action="/cityid.py" method="POST">
<input type="text" name="city_input">
<input type="submit1" name="submit" value="submit">
</form>
within my cityid.py script I have the following to start off the script, referred to this question. Posting html form values to python script
import cgi
def city_details():
form = cgi.FieldStorage()
city = form.getvalue('city_input')
#rest of the script below, but the city variable is what I need to be user input from the flask form
Here's what I have in the main.py file
@app.route("/city_details2", methods=['GET','POST'])
def city_details2():
if request.method == "POST":
return render_template("city_details2.html", city_details=city_details())
Now I can't seem to even start the flask web server because I get the following error:
TypeError: can only concatenate str (not "NoneType") to str
It's basically already trying to run the separate cityid.py script without having the user input first to get the city variable.
Anyone have an input on what I'm doing wrong here? I suspect it's something wrong with the @app.route? Or how do I get the user input first before running the cityid.py script?
Upvotes: 0
Views: 809
Reputation: 3987
I believe this is going to help you.
Here is a simple URL, which passes two values to hello_get.py program using GET method.
/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI
Below is hello_get.py script to handle input given by web browser. We are going to use cgi module, which makes it very easy to access passed informationimport cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields first_name = form.getvalue('first_name') last_name = form.getvalue('last_name')
HTML :
<form action = "/cgi-bin/hello_get.py" method = "get"> First Name: <input type = "text" name = "first_name"> <br /> Last Name: <input type = "text" name = "last_name" /> <input type = "submit" value = "Submit" /> </form>
Upvotes: 0