Ruben Ruben - 9 months ago 66
JSON Question

Cannot fill the WTForm properly with request.args

I'm trying to fill the form through WTForms:

class SearchForm(Form):
dt1 = DateTimeField(validators=[validators.Required()])
dt2 = DateTimeField(validators=[validators.Required()])
count = IntegerField(default=10, validators=[validators.Optional()])
offset = IntegerField(default=0, validators=[validators.Optional()])

@app.route('/rest-api/get_calls/<phone_number>', methods=['GET'])
def get_calls(phone_number):
form = SearchForm(request.args)

The request is like that: - - [24/Oct/2016 23:23:19] "GET /rest-api/get_calls/003223185901?dt1=2016-01-01T00:00:00&dt2=2016-08-31T00:00:00 HTTP/1.1" 404 -

but I get this:

{'dt2': ['This field is required.'], 'dt1': ['This field is required.']}

It looks like request.args is not working properly.

Answer Source

You're not passing the date/time values in the right format. DateTimeField expects %Y-%m-%d %H:%M:%S but you've passed %Y-%m-%dT%H:%M:%S, notice the T. Either pass the expected format or change it with DateTimeField(format='%Y-%m-%dT%H:%M:%S', ...).