Is there any way I can remove this model form file field's 'clear' checkbox?.I know that I can define the 'custom' widget for file field but how to address this checkbox in that?
If yes try to disable this behavior, set the novalidate attribute on the form tag As <form action="{% url 'new_page' %}", method="POST" novalidate> in your html file.
The clean() method on a Field subclass is responsible for running to_python() , validate() , and run_validators() in the correct order and propagating their errors. If, at any time, any of the methods raise ValidationError , the validation stops and that error is raised.
You need to change the widget from the ClearableFileInput to Fileinput https://docs.djangoproject.com/en/dev/ref/forms/widgets/#fileinput
Adding to @schacki's answer. Here's how to use the simpler FileInput
widget:
# forms.py
from django.forms.widgets import FileInput
class SomeForm(forms.Form):
foofile = forms.FileField(widget=FileInput)
The default FileField widget seems to be ClearableFileInput
.
I know this question is quite long, but for anyone still trying to remove the clear checkbox from displaying on form just remove blank=True from the model.
i.e.
myimage = models.Image(blank=True, upload_to='my_profile')
To
myimage = models.Image(upload_to='my_profile')
This is because the blank=True is what makes the checkbox to appear besides the image input in forms.
If you are rendering the ImageField in your template using directly the typical {{ imagefieldname }}
you can easily format it just substituting it by a copy-paste of the HTML generated by Django after rendering the template.
You can see that "Clear" checkbox in the HTML generated by Django and delete it if you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With