Django ClearableFileInput - how to detect whether to delete the file -
i'm using django crispy forms form option upload image (imagefield in model)
the forms renders i'd expect, checkbox clear existing file. when processing form submission 'image-clear' checkbox gives me 'none' value.
image_clear = form.cleaned_data.get("image-clear") print image_clear in html of page notice input checkbox doesn't have value attribute, see:
<input id="image-clear_id" type="checkbox" name="image-clear"> so wondered if issue, when @ rendering in django admin site, corresponding html input field doesn't have value either - yet still identifies image should removed.
i should note if upload new image, works, it's case i'm removing/clearing image (and works in django admin pages, assume means model definition ok allow no image attached model)
so... in form processing, how detect whether or not image should removed or not?
i'm sure i'm missing simple here - , appreciated.
you shouldn't check checkbox, check value of file input field. if false, can delete file. otherwise uploaded file. see: https://github.com/django/django/blob/339c01fb7552feb8df125ef7e5420dae04fd913f/django/forms/widgets.py#l434
# false signals clear existing value, opposed none return false return upload
Comments
Post a Comment