Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value from select tag using flask

Tags:

python

html

flask

I'm new to Flask and I'm having trouble getting the value from my select tag. I have tried request.form['comp_select'] which returns a Bad Request. However, when I try using request.form.get('comp_select'), my return page returns a blank list "[]".

My html:

<form class="form-inline" action="{{ url_for('test') }}">   <div class="form-group">     <div class="input-group">         <span class="input-group-addon">Please select</span>             <select name="comp_select" class="selectpicker form-control">               {% for o in data %}               <option value="{{ o.name }}">{{ o.name }}</option>               {% endfor %}                                                           </select>     </div>     <button type="submit" class="btn btn-default">Go</button>   </div> </form> 

My app.py:

@app.route("/test" , methods=['GET', 'POST']) def test():     select = request.form.get('comp_select')     return(str(select)) # just to see what select is 

Sorry in advance if my formatting is off for the post (also new to Stack Overflow).

like image 872
qwertyuip9 Avatar asked Aug 14 '15 22:08

qwertyuip9


People also ask

How do I get data from select option in Flask?

to use select = request. form. get('comp_select') in the test view to get the value of the select element with the name attribute set to comp_select . to add a form with the select element with the name attribute set to comp_select .

How to get value from select tag using Python Flask?

Sometimes, we want to get value from select tag using Python Flask. In this article, we’ll look at how to get value from select tag using Python Flask. How to get value from select tag using Python Flask? To get value from select tag using Python Flask, we can use the request.form propety in our view.

How to get user input from HTML form in flask?

URL_for is an Flask way of creating dynamic URLs where the first arguments refers to the function of that specific route in flask. In our form it will create a Dynamic route which has gfg function in the flask app Now we will create the flask backend which will get user input from HTML form

What is URL_for in flask?

It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. Its an simple HTML form using the post method the only thing is unique is action URL. URL_for is an Flask way of creating dynamic URLs where the first arguments refers to the function of that specific route in flask.

How to get the value of the select element in test view?

to use select = request.form.get ('comp_select') in the test view to get the value of the select element with the name attribute set to comp_select.


1 Answers

It's hard to know for certain from what you've provided, but I believe you need to add method="POST" to your <form> element.

From the flask doc for the request object:

To access form data (data transmitted in a POST or PUT request) you can use the form attribute. ... To access parameters submitted in the URL (?key=value) you can use the args attribute.

So, if you submit your forms via POST, use request.form.get(). If you submit your forms via GET, use request.args.get().

This app behaves the way you want it to:

flask_app.py:

#!/usr/bin/env python from flask import Flask, flash, redirect, render_template, \      request, url_for  app = Flask(__name__)  @app.route('/') def index():     return render_template(         'index.html',         data=[{'name':'red'}, {'name':'green'}, {'name':'blue'}])  @app.route("/test" , methods=['GET', 'POST']) def test():     select = request.form.get('comp_select')     return(str(select)) # just to see what select is  if __name__=='__main__':     app.run(debug=True) 

templates/index.html

<form class="form-inline" method="POST" action="{{ url_for('test') }}">   <div class="form-group">     <div class="input-group">         <span class="input-group-addon">Please select</span>             <select name="comp_select" class="selectpicker form-control">               {% for o in data %}               <option value="{{ o.name }}">{{ o.name }}</option>               {% endfor %}             </select>     </div>     <button type="submit" class="btn btn-default">Go</button>   </div> </form> 
like image 82
Robᵩ Avatar answered Oct 03 '22 02:10

Robᵩ