Getting Input
template.html:
<!doctype html>
<title>getting input</title>
{% if not submittedYet %}
<form action="/log" method="post">
Enter your username and click Submit:<br>
<input type="text" name="username" value="Yasuo">
<br>
<input type="submit" value="Submit">
</form>
{% else %}
<h1>welcome back {{ user }}</h1>
{% endif %}
site.py:
from flask import Flask
from flask import render_template
from flask import request
app = Flask(__name__)
@app.route("/log", methods=["POST"])
def submittedForm():
return render_template("template.html",
submittedYet = True,
user = request.form["username"])
@app.route("/")
def start():
return render_template("template.html",
submittedYet = False)
app.run(host="0.0.0.0", port=5000)
Challenges
- try GET instead of POST..
change template.html:<form action="/log" method="get">
change site.py:@app.route("/log", methods=["GET"])and:user = request.args.get("username")how does GET differ from POST? what situations might i use either? - try retrieving and displaying data from other simple HTML form controls:
- Radio button
- Select element (aka drop down list)
- Text area
- Checkbox
- Datalist (new in HTML5)
- make a simple paper rock scissors game