Skip to content

Instantly share code, notes, and snippets.

@devdave
Created December 13, 2011 16:39
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save devdave/1472832 to your computer and use it in GitHub Desktop.
Save devdave/1472832 to your computer and use it in GitHub Desktop.
Flask HTML form array inputs
from flask import Flask
from flask import request
app = Flask(__name__)
#Normally this would be an external file like object, but here
#it's inlined
FORM_PAGE = """
<html>
<head>
<title>Flask Form</title>
</head>
<body>
<form action="/process" method="POST">
<label>Name</label>
<input name="user_name" />
<br/>
<label>Age</label>
<input type="range" name="user_age" />
<br/>
<fieldset>
<legend>Drink preferences</legend>
<label>Coffee, Tea, water?</label><br/>
<input type="checkbox" name="drink_type[]" value="coffee" /> Coffee <br/>
<input type="checkbox" name="drink_type[]" value="tea" /> Tea <br/>
<input type="checkbox" name="drink_type[]" value="water" /> Water <br/>
<label>Temperature</label><br />
<input type="radio" name="drink_temp" value="hot" /> Hot <br/>
<input type="radio" name="drink_temp" value="warm" /> Warm <br/>
<input type="radio" name="drink_temp" value="Cold" /> Cold <br/>
<input type="radio" name="drink_temp" value="Iced" /> Iced <br/>
</fieldset>
<input type="submit" />
</form>
</html>
"""
@app.route('/')
def home_form():
return FORM_PAGE
@app.route("/process", methods = ["GET", "POST"] )
def process_form():
formData = request.values if request.method == "GET" else request.values
response = "Form Contents <pre>%s</pre>" % "<br/>\n".join(["%s:%s" % item for item in formData.items()] )
return response
if __name__ == '__main__':
app.run()
@avinassh
Copy link

Hey really nice example for beginners !

@zzl0
Copy link

zzl0 commented Mar 20, 2014

thx for this example!

@vold
Copy link

vold commented Jan 20, 2015

I check : coffee, tea, water .

result : only coffee

FC21, Firefox

@KentaYamada
Copy link

It's really nice example!!

@InfinitySearch
Copy link

InfinitySearch commented Sep 13, 2021

This used to work but the checkbox one only returns one value now for some reason. Using request.form.getlist('name_needed') is another way to retrieve multiple values from a form. Here is an alternate example:

from flask import Flask
from flask import request

app = Flask(__name__)
# Normally this would be an external file like object, but here
# it's inlined
FORM_PAGE = """
    <html>
        <head>
            <title>Flask Form</title>
        </head>
        <body>
            <form action="/process" method="POST">
                <label>Name</label>
                <input name="user_name" />
                <br/>

                <label>Age</label>
                <input type="range" name="user_age" />
                <br/>

                <fieldset>
                    <legend>Drink preferences</legend>

                    <label>Coffee, Tea, water?</label><br/>
                    <input type="checkbox" name="drink_type" value="coffee" /> Coffee  <br/>
                    <input type="checkbox" name="drink_type" value="tea" /> Tea  <br/>
                    <input type="checkbox" name="drink_type" value="water" /> Water  <br/>

                    <label>Temperature</label><br />
                    <input type="radio" name="drink_temp" value="hot" /> Hot <br/>
                    <input type="radio" name="drink_temp" value="warm" /> Warm <br/>
                    <input type="radio" name="drink_temp" value="Cold" /> Cold <br/>
                    <input type="radio" name="drink_temp" value="Iced" /> Iced <br/>
                </fieldset>


                <input type="submit" />                
            </form>
    </html>
"""


@app.route('/')
def home_form():
    return FORM_PAGE


@app.route("/process", methods=["GET", "POST"])
def process_form():
    formData = request.values

    spacing = "<br><br>"

    response = "Form Contents" + spacing
    response += "Name: " + str(formData.get('user_name')) + spacing
    response += "Age: " + str(formData.get('user_age')) + spacing
    response += "Coffee, Tea, water?: " + str(formData.getlist('drink_type')) + spacing
    response += "Temperature: " + str(formData.get('drink_temp')) + spacing

    print(formData.get('user_name'))
    print(formData.get('user_age'))
    print(formData.getlist('drink_type'))
    print(formData.get('drink_temp'))

    return response


if __name__ == '__main__':
    app.run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment