wtforms

What is the correct way to populate select choices from session data?

你。 提交于 2020-11-28 02:50:01
问题 I'm storing some variables in the session when the user logs in, to use later to populate a field. from flask_wtf import Form from wtforms import SelectField from flask import session class InstitutionForm(Form): city = session['city'] city_tuples = [(x, x) for x in city] organisation = SelectField( 'organisation', choices=city_tuples ) class Institution(View): methods = ['POST'] def dispatch_request(self): form = InstitutionForm() return render_template( 'form/institution.html', form=form)

How can I create repetitive form elements in a DRY way with Flask-WTForms?

怎甘沉沦 提交于 2020-08-25 04:56:06
问题 I have a WTForms form where I want the user to be able to upload up to 10 images, and also give the images captions and credits. Currently I declare all 10 sets of fields, but this seems redundant. Is there a way to create form fields with dynamic names, so I could create them in a loop? class MyForm(Form): image1 = FileField('Upload') image1_caption = StringField('Caption') image1_credit = StringField('Credit') image2 = FileField('Upload') image2_caption = StringField('Caption') image2