TextField in flask-wtforms returns an empty string even after entering the date

2.9k Views Asked by At

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.

2

There are 2 best solutions below

1
On

Do u have a {{ form.csrf_token }} in your template (u can put it in you form). 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.

0
On

The following code should be useful:

from flask import Flask, render_template, request
from wtforms import Form
from wtforms.fields import DateField
from wtforms.validators import Required


app = Flask(__name__)
app.config['DEBUG'] = True


class TestForm(Form):
    foo_date = DateField('Foo Date',
                         [Required('Foo Date is required')],
                         format='%Y-%m-%d',
                         description='Date format: YYYY-MM-DD')

@app.route("/", methods=['GET', 'POST'])
def index():
    submitted_date = None
    if request.method == 'POST':
        form = TestForm(request.form) # PAY ATTENTION HERE
        if form.validate():
            submitted_date = form.foo_date.data
    else:
        form = TestForm()

    return render_template('tpl.html', form=form, submitted_date=submitted_date)


if __name__ == "__main__":
    app.run()

Template tpl.html (in the 'templates' folder):

<!DOCTYPE html>
<html>
<body>
<form method='POST' action=''>
    <p>{{ form.foo_date.label }}: {{ form.foo_date }} {{ form.foo_date.description }}</p>
    <input type='submit' value='Send foo date'>
</form>
{% if submitted_date %}
<p style="color: green">You submitted successfully the following date: {{ submitted_date }}</p>
{% endif %}
{% if form.errors %}
<h2>Errors</h2>
<p style="color: red">{{ form.errors }}</p>
{% endif %}
</body>
</html>

Be careful, you haven't initialized the form class correctly when the request method is POST (see my code).