I have created a form using flask wtforms containing TextField and DateField . Here is my form class:
class SubmitReportForm(Form):
projectName=TextField('Name of Project', [Required('Please enter name of the project')])
workDone=TextAreaField('work', [Required('Please state your progress')])
fromDate=DateField('fromDate', [Required('Please mention a start date')])
toDate=DateField('toDate', [Required('Please mention an end date')])
submit=SubmitField('Submit')
My view function dealing with this form is :
@app.route('/user/<userName>/submit', methods=['GET', 'POST'])
@login_required
def submit(userName):
form=SubmitReportForm()
if request.method=='GET' :
return render_template("submit.html", userName=userName, form=form)
elif request.method =='POST' :
if form.is_submitted():
print 'submitted'
if form.validate():
print 'validated'
print form.errors
if form.validate_on_submit():
project=form.projectName.data
fromDate=form.fromDate.data
toDate=form.toDate.data
progress=form.workDone.data
report=writeToFile(current_user.userName, project, fromDate, toDate, progress)
recipient=['[email protected]']
subject="Monthly report by : " + current_user.userName
msg = Message(subject, sender =(current_user.userName, '[email protected]'), recipients = recipient)
msg.body= "Please find the attached report by "+ current_user.userName
with app.open_resource(report.name) as fp:
msg.attach(report.name, "text/plain", fp.read())
mail.send(msg)
return render_template('successSubmit.html')
else:
flash(u'Please fill all the fields', 'error')
return render_template("submit.html", userName=userName, form=form)
Now when I click the submit button, the form.validate_on_submit() always returns false. After some debugging I found that the form is submitted but not validated because the form.fromDate.data always returns a None type object even after the date is entered in the form.
My HTML file:
{% extends 'base.html' %}
{% block content %}
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<p><span style="color: red;">{{ message }}</span></p>
{% endfor %}
{% endif %}
{% endwith %}
<form action ='{{url_for('submit', userName=userName)}}' method='POST'>
{{form.hidden_tag()}}
<p>
Project Name: {{form.projectName}}
</p>
<br>
<p>
<label>Start Date : </label> {{form.fromDate}}
</p>
<br>
<p>
<label>End Date : </label> {{form.toDate}}
</p>
<br>
<p>
Progress Done: {{form.workDone(style="width: 699px; height: 297px;")}}
</p>
<br>
<p>
<input type='submit' value='Send Report'>
</p>
<br>
</form>
{% endblock %}
Even if I use TextField in place of DateFields, I get an empty string. So please tell me where am I going wrong ?? Thanx in advance.
Do u have a
{{ form.csrf_token }}
in your template (u can put it in youform
). Just give it a try, maybe work... If you have done, then... I've just met a problem, it's caused by the CSRF, so I add this variable and it worked.