Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django:any way to remove this clear field?

Tags:

python

django

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?enter image description here

like image 808
Rajat Saxena Avatar asked Aug 14 '12 10:08

Rajat Saxena


People also ask

How do I get rid of this field required Django?

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.

What does clean do in Django?

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.


4 Answers

You need to change the widget from the ClearableFileInput to Fileinput https://docs.djangoproject.com/en/dev/ref/forms/widgets/#fileinput

like image 58
schacki Avatar answered Oct 27 '22 01:10

schacki


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.

like image 39
Banjer Avatar answered Oct 27 '22 02:10

Banjer


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.

models

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.

Hope this helps

like image 33
Micheal N.D Avatar answered Oct 27 '22 02:10

Micheal N.D


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.

like image 38
mvillaress Avatar answered Oct 27 '22 00:10

mvillaress