Skip to content Skip to sidebar Skip to footer

Passing Value From A Drop Down Menu To A Flask Template

I am having an issue passing an item selected from an HTML drop down menu to a SQL query. I'm not exactly sure what concept is missing from my code to make this work. Most examples

Solution 1:

You need to put your select inside the form.

<formname="Item_1"action="results.html"method='POST'><selectname="Item_1"><optionvalue="Red">Red</option><optionvalue="Green">Green</option></select><buttontype="submit">Compare!</button></form>

An even better way to declare the form would be

<formname="Item_1"action="{{ url_for('results') }}"method="POST">

Solution 2:

As mentioned by @dim put the select inside a the form and the chosen value can be got using request.form['Item_1']. However the item being queried is a select, so I would prefer using get instead of POST. From wiki, http://en.wikipedia.org/wiki/POST_(HTTP), POST is used when

The POST request method is designed to request that a web server accept the data enclosed in the request message's body for storage.[1] It is often used when uploading a file or submitting a completed web form.

So I would rather prefer GET, which can be used to query the database. When using GET, Item_1 can be passed as request parameters and got using flask.request.args

Post a Comment for "Passing Value From A Drop Down Menu To A Flask Template"