Get an uploaded file from a WTForms field

request.files is a dictionary where the keys are the names of the file fields. You can get the name of a WTForms field with my_form.my_field.name. So you can access the data uploaded from that field with request.files[my_form.my_field.name]. Rather than using the WTForms FileField, you can use the Flask-WTF FileField instead. It provides a data attribute … Read more

Flask WTForms: Difference between DataRequired and InputRequired

Short Answer Unless you have a good reason you should use InputRequired Why? Lets look at some notes from the docs/code for DataRequired() : Note there is a distinction between this and DataRequired in that InputRequired looks that form-input data was provided, and DataRequired looks at the post-coercion data. and NOTE this validator used to … Read more

How to render my TextArea with WTForms?

Very old question, but since the WTF-Form documentation isn’t clear I’m posting my working example. OP, hope you are not still working on this. 🙂 form from flask_wtf import Form from wtforms.fields import StringField from wtforms.widgets import TextArea class PostForm(Form): title = StringField(u’title’, validators=[DataRequired()]) body = StringField(u’Text’, widget=TextArea()) template {% extends “base.html” %} {% block … Read more

WTForms getting the errors

The actual form object has an errors attribute that contains the field names and their errors in a dictionary. So you could do: for fieldName, errorMessages in form.errors.items(): for err in errorMessages: # do something with your errorMessages for fieldName

Recommendation for python form validation library [closed]

Disclaimer Generally speaking I’m a little wary about HTML form libraries now. If you use something from a mega-framework, you invariably have to bring in the whole mega-framework as your dependency. Many sub-components of many mega-frameworks claim to not depend on the framework but let’s not kid ourselves. If you don’t use one, there are … Read more

Flask-WTF – validate_on_submit() is never executed

You’re not inserting the CSRF field in the HTML form. <form method=post> {{ form.csrf_token }} {{ form.name }} <input type=submit> </form> After adding form.csrf_token to the template (docs), the form will validate as expected. Add print(form.errors) after validating the form to see the errors that were raised. errors will be empty before validation. In this … Read more

Not a Valid Choice for Dynamic Select Field WTFORMS

My guess is that Area.id is a int – when data comes back from the client it is treated as a string by WTForms unless a callable is passed to the coerce keyword argument of the wtforms.fields.SelectField constructor: area = SelectField(coerce=int) Alternately, if you are using SQLAlchemy you could use wtforms.ext.sqlalchemy.fields.QuerySelectField (wtforms_sqlalchemy if you are … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)