I am new to Flask. I have a simple page which takes some inputs & redirect to same page with inputs displayed this works fine with render template method but I want to avoid this as with every refresh it asks for resubmission so I am trying with redirect url_for method , the page gets redirected successfully but form data is not available. How can I fix this?
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
form_data = request.form
print(len(form_data))
return render_template('index.html',form_data=form_data) # this works
# return redirect(url_for('index',form_data=form_data)) this doesn't work
return render_template('index.html',form_data=[])
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000, debug=True)
html
<form action="" method="POST">
<label for="cars" class="form-label">Select Employee Name</label>
<select name="Employee Name" id="Employee Name" required class="form-control">
<option value="none" selected disabled hidden>Select Employee Name</option>
<option value="saab">Saab</option>
<option value="mercedes">John</option>
<option value="audi">mark</option>
</select>
<label for="submission Date" class="form-label">Select Submission Date</label>
<input id="submission Date" type="date" name="Submission Date" class="form-control" required />
<label for="Submission Status" class="form-label">Select Submission Status</label>
<select name="Submission Status" id="Submission Status" required class="form-control">
<option value="volvo">Pending</option>
<option value="saab">Submitted</option>
</select>
<button type="submit" class="btn btn-primary form-control m-2">
Login
</button>
</form>
<div>
{{form_data}} {% if form_data|length
<=0 %} {{form_data}} {% else %} {% for x,y in form_data.items() %} <h1>{{x}}{{y}}</h1>
{% endfor %} {% endif %}
</div>