user4052054
user4052054

Reputation: 378

Invalid form with FileField

I have an UpdateView with an object which has a FileField. If the model has a file uploaded, I show the link to the file, and also a file input in case the user wants to update it.

<form method="post" enctype="multipart/form-data">
    {% if object.file %}<a href="{{ object.file.url}}">Download</a>{% endif %}
    <input type="file" name="{{ form.file.name }}">
    <input type="text" name="{{ form.other_field.name }}">
</form>

which works fine if the object is new and does not have a file, in which case the Download link does not show or if it has a file, in which case the Download link shows up.

But, if I select a file to upload, and the form is invalid, the object.file field exists, and it points to a non-existent file.

Is there a way to tell in an invalid form if the file is real?

Upvotes: 0

Views: 129

Answers (1)

Jorge Mauro
Jorge Mauro

Reputation: 383

You should use enctype

<form method="post" enctype="multipart/form-data">

Upvotes: 1

Related Questions