Reputation: 4907
I have an EmployeeForm
with one of the fields comprising of a PhotoForm
shown below:
class EmployeeForm(Form):
name = StringField('Full Name', validators=[DataRequired()])
title = StringField('Job Title', validators=[DataRequired()])
email = StringField('Company Email', validators=[DataRequired()])
department = StringField('Department', validators=[DataRequired()])
photo = FormField(PhotoForm)
Depending on whether a USE_S3
config variable is set to True
or False
I want two fields on my photo object to be automatically set to default values like so:
class PhotoForm(Form):
image = S3ImageUploadField(base_path=app.config['UPLOAD_FOLDER'],namegen=photo_name_generator)
if app.config['USE_S3']:
image_storage_type = HiddenField('s3')
image_storage_bucket_name = HiddenField(app.config['S3_BUCKET_NAME'])
else:
image_storage_type = HiddenField('empty string')
image_storage_bucket_name = HiddenField('empty string')
However, when I check the form's html there is no hidden field nor when I check the database on submission is there any set values on the image_storage_type
and image_bucket_name
and I can't figure it out why that is. I've checked on some stackoverflow questions but they don't exactly fit my question because I am using Flask-Admin
and WTForm
together which in my opinion makes it a little more trickier.
Also, my admin.py
file for the form rendering looks like this:
class EmployeeView(ModelView):
form = EmployeeForm
I'm not sure if this is a problem on WTForm's side or Flask-Admin's side but I've been trying for a long time to make this work. Does anyone has any ideas? Help would be greatly appreciated.
Upvotes: 0
Views: 3273
Reputation: 4137
The first argument for a field is its label. In your code you aren't passing data into the field, you are setting its label. Set the default data with the default
kwarg.
from wtforms import Form, HiddenField
class PhotoForm(Form):
default_image_data = "foo" if True else "bar"
image_storage_type = HiddenField('image storage', default=default_image_data)
>>> form = PhotoForm()
>>> form.image_storage_type.data
'foo'
>>> form.image_storage_type()
u'<input id="image_storage_type" name="image_storage_type" type="hidden" value="foo">'
Upvotes: 5