-1

I am creating a Flask Application that retrieves data from a csv, one of the columns in the csv is a list of datetime.date() objects, I wrote a piece of code that stores all the dates in a list

For reference I not posting the code of how I retrieved the dates from the CSV, so I have hard coded the dates list here

so here is how the dates list looks like dates=['08-23-2020', '08-24-2020', '08-25-2020', '08-26-2020', '08-27-2020', '08-28-2020', '08-29-2020', '08-30-2020', '08-31-2020', '09-01-2020'] there are many more but just for the sake of presenting it here I have shown few.

So here is my app.py file where I am trying to retrieve the data from the dropdown button, the DailyStats function is where I am trying to display the date selected using the dropdown button

@app.route('/DailyStats', methods=['GET','POST'])
def DailyStats():
    dates=['08-23-2020', '08-24-2020', '08-25-2020', '08-26-2020', '08-27-2020', '08-28-2020', '08-29-2020', '08-30-2020', '08-31-2020', '09-01-2020']
    if request.method=='POST':
        dateSelect = request.form['selectedDate']
        print(dateSelect)
        return redirect(url_for('next'))


    return render_template('DailyStats.html', dates=dates)

@app.route('/next', methods=['GET','POST'])
def next():
    return render_template('next.html')

Here is my html file where I wrote code to create the dropdown(DailyStats.html)

<form method="GET" action="/DailyStats"> 
     <select name="selectedDate">
         {% for d in dates %}
                <option value= "{{d}}" SELECTED>{{d}}</option>"
         {% endfor %}
     </select>
    <input type="submit" name="" value="submit">
</form>  

And I want to display the date selected in my next.html file but when I select the date it doesn't print dateSelect variable and it is not redirected to the next page as well

My URL shows me something like this http://127.0.0.1:5000/DailyStats?selectedDate=08-23-2020
for your reference here is a snippet of my DailyStats.html page enter image description here

Also, I definitely know there is something I am doing wrong because I am new to Flask.

Glen Veigas
  • 186
  • 11

1 Answers1

1

Try changing this line:

<form method="GET" action="/DailyStats">

to this (using method='POST'):

<form method="post" action="/DailyStats">
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30