I am currently trying to build a simple web application using Flask. With this i am also using WTForms, however i am having problem with getting date information from the form and getting it validated.
This is the form:
from flask_wtf import FlaskForm
from wtforms import SubmitField
from wtforms.fields.html5 import DateField
from wtforms.validators import DataRequired
from datetime import date
class LeasForm(FlaskForm):
start_date = DateField("Start date", default=date.today(), format='%d/%m/%Y', validators=[DataRequired(message="You need to enter the start date")],)
end_date = DateField("End date", validators=[DataRequired(message="You need to enter the end date.")], format='%d/%m/%Y')
submit = SubmitField("To payment")
Then in routes i have the following:
@app.route('/url/<int:some_id>', methods=['GET', 'POST'])
def some_route(some_id):
....
form = LeasForm()
print("Request form: {}".format(request.form))
print("Start date data: {}".format(form.start_date.data))
print("End date data: {}".format(form.end_date.data))
print("Leas form: {}".format(form.validate()))
print("Leas form errors: {}".format(form.errors))
if form.validate():
return redirect(url_for('another_url'))
....
and in the view:
....
<form action="" method="post">
<div>{{form.errors}}</div>
{{ form.hidden_tag() }}
{{ form.start_date.title}}
{{ form.start_date}}
{{ form.end_date.title}}
{{ form.end_date}}
{{ form.submit}}
</form>
but here comes the problem, when the form is submitted and i try to get the data it says it is none. This is the output that is given from the print statements in route:
Request form: ImmutableMultiDict([('csrf_token', 'CHANGED_TOKEN'), ('start_date', '2018-04-04'), ('end_date', '2018-04-06'), ('submit', 'To payment')])
Start date data: None
End date data: None
Leas form: False
Leas form errors: {'start_date': ['You need to enter the start date'], 'end_date': ['You need to enter the end date.']}
I have tried to find the answer in both the WTForms docs and using google with no result.
Thanks in advance and just send a message or comment if more information is needed.
DateExample.py
DateExample.html