1

Starting to beat my head against the wall...perhaps I am missing something simple.

models.py

class GFImage(models.Model):
    image = models.ImageField(upload_to = 'uploads', null=True, blank=True)

views.py

def addImage(request):
errors = []
if request.method == 'POST':
    form = ImageForm(request.POST, request.FILES)
    if form.is_valid():
        form.save()
        urlRedirect = "/home"
        return redirect(urlRedirect)
else:
    form = ImageForm()
return render(request, "/add_image.html", {'form': form})

forms.py

class ImageForm(ModelForm):
    class Meta:
        model = GFImage

add_image.html

<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    <table>
        {{ form.as_table }}
    </table>
    <input type = "submit" value = "Submit">

</form>

Whatever I do, my form will not use the ClearableFileInput widget. It should default automatically, but even assigning it in the form's META will not work. What else could be blocking Django from using the clearable widget?

John D.
  • 548
  • 7
  • 19
  • I can't find any mention in the documentation about `ClearableFileInput` being the default widget for `ImageForm` - could you point me to it? – Ludwik Trammer Nov 24 '13 at 20:30
  • ClearableFileInput class ClearableFileInput File upload input: , with an additional checkbox input to clear the field’s value, if the field is not required and has initial data. – John D. Nov 24 '13 at 23:26

1 Answers1

1

The ClearableFileInput will only display the clear checkbox when there's an initial file selected. Looking at your form, it looks like a a new form without initial data, so the checkbox won't be displayed.

def render(self, name, value, attrs=None):
    .. snip ..
    if value and hasattr(value, "url"):
        template = self.template_with_initial
        substitutions['initial'] = format_html(self.url_markup_template,

https://github.com/django/django/blob/5fda9c9810dfdf36b557e10d0d76775a72b0e0c6/django/forms/widgets.py#L372

tuxcanfly
  • 2,494
  • 1
  • 20
  • 18
  • 1
    That makes sense now. I was not thinking clearable in relation to the database, but clearable in relation to the entry field on the form. I have solved my need with a button and bit of jquery. – John D. Nov 25 '13 at 22:20