WTF Know how

Aus Froggis Wissenssammlung
Wechseln zu:Navigation, Suche

How to get WTF results as Integer?

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) <-- changes the default coerce to Integer

Alternately, if you are using SQLAlchemy you could use wtforms.ext.sqlalchemy.fields.QuerySelectField (wtforms_sqlalchemy if you are using WTForms 3+):


  area = QuerySelectField(query_factory=Area.objects.all,
                           get_pk=lambda a: a.id,
                           get_label=lambda a: a.name)


Formularfehler anzeigen lassen

 for fieldName, errorMessages in form.errors.items():
     for err in errorMessages:
         flash("In field " + fieldName + " you have an error: " + err, 'danger')
         pprint("In field " + fieldName + " you have an error: " + err, 'danger')