Reputation: 849
I'm trying to get value from dropdown box into variable and then store it. I'm new to Flask and cant find anything in the documents about it. But I dont know how to get the value from dropdown list with request.form or any other why in that matter.
My flask code in app.py
@app.route('/add', methods=['POST'])
def add_entry():
if not session.get('logged_in'):
abort(401)
title = request.form['title']
link = request.form['link']
shown = request.form['shown']
#I hardcoded the id here too see basic function.
kate = Category.query.filter_by(id = 2).first()
add_into = Entries(title, link, shown, kate)
db.session.add(add_into)
db.session.commit()
And here is the html for it.
<form action="{{ url_for('add_entry') }}" method=post class="add-entry custom">
<dl>
<dt>Title:
<dd><input type=text size=120 name=title>
<dt>Link:
<dd><input type=text size=120 name=link>
<dt>Shown:
<dd><input type=text size=120 name=shown>
<label for="customDropdown">Category</label>
<select style="display:none;" id="customDropdown">
{% for c in cate %}
{% if c.id == 1 %}
<option id="{{ c.name }}" name="{{ c.name }}" SELECTED>{{ c.name }}</option>
{% else %}
<option>{{ c.name }}</option>
{% endif %}
{% endfor %}
</select>
<dd><input class="success button" type=submit value=Post it!>
</dl>
</form>
Upvotes: 4
Views: 16821
Reputation: 43071
A select tag, like an input tag, needs a "name" attribute on it if you wish to have the value passed as form data.
<select name="name" style="display:none;" id="customDropdown">
Now, you should be able to access it through request.form['name']
as you have been your input elements.
Upvotes: 7