wtforms/wtforms

FileField set value in edit form

aminalaee opened this issue · 2 comments

This is more of a question than an issue, but I couldn't find any other place to ask this.

For the following example:

>>> import wtforms
>>> class F(wtforms.Form):
...     image = wtforms.FileField()

Is there any way to set the FileField current value, for example in the edit page of some object? I have tried the following but doesn't work:

form = F()
form.image.data = open("...", "rb").read()

But this doesn't work. Is there any way to do this?

azmeuk commented

Hi @aminalaee
What do you mean by "this doesn't work"?

>>> import wtforms
>>> class F(wtforms.Form):
...     image = wtforms.FileField()
>>> form = F()
>>> form.image.data = b"xxx"
>>> form.image.data
b'xxx'

Can you provide a copy-pastable snippet that demonstrates the behavior your are referring to?

TBH I don't remember the issue but I think it was about an edit input type which you can't set an existing value but it is related to HTML not WTForms.