Skip to content Skip to sidebar Skip to footer

Unable To Get Value Selected In Python From A Dropdown Using Flask

This question could be a duplicate but I have checked all the answers of such related questions and I haven't been able to solve it. I am trying to get the value from a dropdown me

Solution 1:

There are several ways to achieve this. Either you can give logic to the template itself or you can add the logic in the function threshold.

index.html

<h2> {{text}} </h2>
<form action= "{{ url_for('threshold') }}" method="POST">

     <select name= 'tvalue'>
      {% for tvalue in tvalues %}
        {% if selected_tvalue == tvalue %}
            <option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>
        {% else %}
             <option value="{{ tvalue }}" >{{ tvalue }}</option>
        {% endif %}
      {% endfor %}

      </select>

     <input type="submit" value="Submit" />
</form>

OR,

{% if selected_tvalue > 3 %}
    <h2> Selected value is greater than 3 </h2>
{% else %}
     <h2> Selected value is less than or equal to 3 </h2>
{% endif %}   
<form action= "{{ url_for('threshold') }}" method="POST">

     <select name= 'tvalue'>
      {% for tvalue in tvalues %}
        {% if selected_tvalue == tvalue %}
            <option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>
        {% else %}
             <option value="{{ tvalue }}" >{{ tvalue }}</option>
        {% endif %}
      {% endfor %}

      </select>
    <input type="submit" value="Submit" />
</form>

server.py

def template(title = "HELLO!", text = ""):
    templateDate = {
        'text' : text,
        'tvalues' : getTValues(),
        'selected_tvalue' : -1
    }
    return templateDate

def getTValues():
    return (10, 11, 15, 2, 1) 

@app.route("/threshold", methods=['POST', 'GET'])
def threshold():
    tvalue= -1 #default value
    msg = ''
    if request.method == "POST":            
        tvalue = int(request.form['tvalue'])
        if tvalue> 3:
            msg= "rating above 3"

    #generating template data
    templateData = template(text = msg)
    templateData['selected_tvalue'] = tvalue 

    return render_template('index.html', **templateData)

Then access your form at path /threshold. I hope it helps.


Solution 2:

In your html after your drop down block you may need something like

 <input type="submit">

which will trigger the submit. I am not sure, selecting a value alone triggers the form submit.

By the way where are you rendering your page initially ? I would have something like:

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')

in the python code. There to get the value, I would try

tvalue= request.args.get('tvalue')

Well not 'form' but 'args', and normal brackets instead of squared ones. Finally the function where you are going to handle that 'templateData' might be missing too.

last note: you might need GET method too:

@app.route("/threshold", methods=['GET', 'POST'])

Post a Comment for "Unable To Get Value Selected In Python From A Dropdown Using Flask"