# notice we're importing the 'request' variable too! from flask import Flask, request app = Flask(__name__) # these are just our HTML templates. scroll down for the code. formpage = """ Basic form
Enter a number:
Enter a string:
""" presentpage = """ Present data! You entered this number: {number}
You entered this string: {string} """ # the default for methods is ["GET"]. # you can have one function handle multiple methods, or have different functions # for different methods, or whatever you want. @app.route("/", methods=["GET", "POST"]) def form(): # use the request.method attribute to figure out whether this is a GET or a POST. if request.method == "POST": # notice the {number} and {string} in presentpage above - that's what these # names are referencing. return presentpage.format( # the request.form dict holds the decoded form data from the POST body. # the come from the input name attributes in the HTML. number = request.form["anumber"], string = request.form["astring"] # we could use request.form.get("anumber") to get the number or None. # in that case, you would not get a 400 Bad Request, since no error is raised. ) else: return formpage if __name__ == "__main__": app.run()