Created
May 23, 2017 14:27
-
-
Save chrisbay/d905bad4eb4b3bb1a4421127e93c193d to your computer and use it in GitHub Desktop.
Flask handlers to display and process various form inputs
This file contains 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