Miles Law
Miles Law

Reputation: 71

How to access value from select tag using Flask?

I'm trying to get the value inside my select tag using Flask. I tried doing this for using an input tag and it works but I'm not sure how to do it with the select tag. With this HTML code:

<form method="POST">
    <p>
        <input type="text" name="input">
        <input type="submit" value="Submit">
    </p>
</form>

and this python code:

@app.route('/',methods=['POST'])
def run():
    printHTML(request.form['input'])
    return render_template('home.html')

I'm able to access the input text with flask, but for some reason when I try to do the same thing with the select tag, it doesn't work.

HTML code:

<form method="POST"> 
<select name ="startYear">
    <option value="" disabled="disabled"  style="display:none" selected="selected">Please select a year</option>
    <option value="2015">2015</option>
    <option value="2016">2016</option>
    <option value="2017">2017</option>
    <option value="2018">2018</option>
    <option value="2019">2019</option>
    <option value="2020">2020</option>
</select>
<select name ="startMonth">
    <option value="" disabled="disabled"  style="display:none" selected="selected">Please select a month</option>
    <option value="01">January</option>
    <option value="02">February</option>    
    <option value="03">March</option>                       
    <option value="04">April</option>
</select>
</form>

and this python code:

    from flask import Flask, render_template, redirect, request
from xml_identifiersearch import searchXML, printHTML
from xml_time import sortDate, returnDateFile, dateRange, searchXML, printDateHTML

app = Flask(__name__)

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

@app.route('/',methods=['POST'])
def run():
    printHTML(request.form['input'])
    return render_template('home.html')

@app.route('/',methods=['POST'])
def run2():
    startYear=request.form['startYear']
    startMonth=request.form['startMonth']
    startDay=request.form['startDay']
    startHour=request.form['startHour']
    startMinute=request.form['startMinute']
    startSecond=request.form['startSecond']
    startMillisecond=request.form['startMillisecond']
    endYear=request.form['endYear']
    endMonth=request.form['endMonth']
    endDay=request.form['endDay']
    endHour=request.form['endHour']
    endMinute=request.form['endMinute']
    endSecond=request.form['endSecond']
    endMillisecond=request.form['endMillisecond']
    startDatetime=startYear+"-"+startMonth+"-"+startDay+"T"+startHour+":"+startMinute+":"+startSecond+"."+startMillisecond+"Z"
    endDatetime=endYear+"-"+endMonth+"-"+endDay+"T"+endHour+":"+endMinute+":"+endSecond+"."+endMillisecond+"Z"
    printDateHTML(startDatetime, endDatetime)
    return render_template('home.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000,debug=True)

This doesn't allow me to access the values startYear and startMonth.

Upvotes: 3

Views: 8794

Answers (1)

Miles Law
Miles Law

Reputation: 71

I figured it out. It didn't work because I had two forms on the same html page. If I just put them together into one form tag, it worked.

Upvotes: 3

Related Questions