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_credit = StringField('Credit')
    # ...through 10 images...

回答1:


You can get what you're looking for by combining FormField with FieldList:

class ImageForm(Form):
    image = FileField('Upload')
    caption = StringField('Caption')
    credit = StringField('Credit')

class MyForm(Form):
    images = FieldList(FormField(ImageForm), min_entries=10)

You can then access the individual ImageForm instances either through my_form_instance.images.entries or by iterating over my_form_instance.images:

for image in my_form_instance.images:
    print(image.data['caption'], image.data['credit'])


来源:https://stackoverflow.com/questions/37801634/how-can-i-create-repetitive-form-elements-in-a-dry-way-with-flask-wtforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!