-
-
Save steven-barkley/341cefe7896b6483c26c6d3f41435948 to your computer and use it in GitHub Desktop.
Flask handlers to display and process various form inputs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request | |
app = Flask(__name__) | |
app.config['DEBUG'] = True | |
@app.route("/form-inputs") | |
def display_form_inputs(): | |
return """ | |
<style> | |
br {margin-bottom: 20px;} | |
</style> | |
<form method='POST'> | |
<label>type=text | |
<input name="user-name" type="text" /> | |
</label> | |
<br> | |
<label>type=password | |
<input name="user-password" type="password" /> | |
</label> | |
<br> | |
<label>type=email | |
<input name="user-email" type="email" /> | |
</label> | |
<br> | |
<input name="shopping-cart-id" value="0129384" type="hidden" /> | |
<br> | |
<label>Ketchup | |
<input type="checkbox" name="cb1" value="first-cb" /> | |
</label> | |
<br> | |
<label>Mustard | |
<input type="checkbox" name="cb2" value="second-cb" /> | |
</label> | |
<br> | |
<label>Small | |
<input type="radio" name="coffee-size" value="sm" /> | |
</label> | |
<label>Medium | |
<input type="radio" name="coffee-size" value="med" /> | |
</label> | |
<label>Large | |
<input type="radio" name="coffee-size" value="lg" /> | |
</label> | |
<br> | |
<label>Your life story | |
<textarea name="life-story"></textarea> | |
</label> | |
<br> | |
<label>LaunchCode Hub | |
<select name="lc-hub"> | |
<option value="kc">Kansas City</option> | |
<option value="mia">Miami</option> | |
<option value="ri">Providence</option> | |
<option value="sea">Seattle</option> | |
<option value="pdx">Portland</option> | |
</select> | |
</label> | |
<br> | |
<input type="submit" /> | |
</form> | |
""" | |
@app.route("/form-inputs", methods=['POST']) | |
def print_form_values(): | |
resp = "" | |
for field in request.form.keys(): | |
resp += "<b>{key}</b>: {value}<br>".format(key=field, value=request.form[field]) | |
return resp | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment