Problem re-rendering ClearableFileInput on validation error

130 views
Skip to first unread message

Michael Goffioul

unread,
Dec 11, 2020, 10:38:03 AM12/11/20
to Django users
When a form containing a ClearableFileInput is re-rendered (e.g. due to validation error of some other field), and the clear checkbox was checked on the first submit, the widget is rendered without initial value, as empty file input.

For instance, let's says there's a model with ImageField and all the form/view mechanics to edit the model. You edit a model instance that has an uploaded image already:
1. on initial render, the field is rendered with the clear checkbox and link to existing file
2. submit the form with some invalid data (e.g. there's another field in the form that does not validate)
3. on re-render, the field is rendered as empty file input, without clear checkbox and link

AFAICS, this is because:
- ClearableFileInput.value_from_datadict() returns False
- FileField.bound_data() returns False: it only returns initial data if submitted data is None or FILE_INPUT_CONTRADICTION
- BoundField.value() returns False
- BoundField.as_widget() renders field with value set to False

I find it non-intuitive that the field is re-rendered as blank, as if there was no initial data. Is this the intended behavior? Or am I missing something?

Michael.

Michael Goffioul

unread,
Dec 11, 2020, 11:13:39 AM12/11/20
to django...@googlegroups.com
Note that if the user decides to upload a new file on step 2, the field will then be re-rendered with the uploaded file object, which also ends up rendering a blank file input. Is there a case where FileField.bound_data() should return anything else than the initial value?



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4133479d-8781-48d5-9e13-8080504a5749n%40googlegroups.com.

Jesus Antonio Sv

unread,
Oct 8, 2021, 10:16:42 AM10/8/21
to Django users
Did you find a solution for this?
Reply all
Reply to author
Forward
0 new messages