Getting file selected for upload to survive validation.

9 views
Skip to first unread message

Ray Cote

unread,
Mar 4, 2008, 1:58:34 PM3/4/08
to django...@googlegroups.com
I have file uploads working using newforms.

However, if I run into validation errors on the form (say a mandatory
field is not filled), then when the form re-displays for the user,
the file they previously selected is no longer selected.

I've been reading through a variety of file upload suggestions, but
I've not found anything specifically targeting this problem.
Any suggestions as to what I'm not doing is appreciated.

Here's what my (simplified) model/view/template looks like:

Model:
class Note(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=80, unique=False)
article = models.FileField(upload_to='articles/%Y_%m/',
null=True, blank=True)

View:
def add(request):
NoteFormClass = forms.models.form_for_model(Note)

if request.POST:
form = NoteFormClass(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
form = NoteFormClass()

response = render_to_response('notes/add.html',
{
'form': form,
})

return response


Template:
<form action="." method="post" enctype="multipart/form-data">
<fieldset>
<table>
<tr>

<td><strong>{{form.category.label}}*:</strong></td><td>{{form.category.errors}}{{cat_combo|safe}}</td>
</tr>
<tr>

<td><strong>{{form.title.label}}*:</strong></td><td>{{form.title.errors}}{{form.title}}</td>
</tr>
<tr>
<td><strong>Article
File:</strong></td><td>{{form.article.errors}}{{form.article}}{{form.article_file}}</td>
</tr>
</table>
</fieldset>
<input type="submit" value="Add" />
</form>


--

Raymond Cote
Appropriate Solutions, Inc.
PO Box 458 ~ Peterborough, NH 03458-0458
Phone: 603.924.6079 ~ Fax: 603.924.8668
rgacote(at)AppropriateSolutions.com
www.AppropriateSolutions.com

Malcolm Tredinnick

unread,
Mar 4, 2008, 2:31:15 PM3/4/08
to django...@googlegroups.com

On Tue, 2008-03-04 at 08:58 -0500, Ray Cote wrote:
> I have file uploads working using newforms.
>
> However, if I run into validation errors on the form (say a mandatory
> field is not filled), then when the form re-displays for the user,
> the file they previously selected is no longer selected.

This is normal browser behaviour: they don't redisplay any file
specified in file upload widgets to avoid phishing-style attacks. So
Django doesn't bother sending it through.

If you search in the archives, I seem to remember some people came up
with ways to pass the filename back in any case for manual display if
you want that, but it still won't be submitted as part of them form.

Regards,
Malcolm

--
Telepath required. You know where to apply...
http://www.pointy-stick.com/blog/

Ray Cote

unread,
Mar 4, 2008, 5:06:31 PM3/4/08
to django...@googlegroups.com, Malcolm Tredinnick
At 1:31 AM +1100 3/5/08, Malcolm Tredinnick wrote:
>On Tue, 2008-03-04 at 08:58 -0500, Ray Cote wrote:
>> I have file uploads working using newforms.
>>
>> However, if I run into validation errors on the form (say a mandatory
>> field is not filled), then when the form re-displays for the user,
>> the file they previously selected is no longer selected.
>
>This is normal browser behaviour: they don't redisplay any file
>specified in file upload widgets to avoid phishing-style attacks. So
>Django doesn't bother sending it through.
>
>If you search in the archives, I seem to remember some people came up
>with ways to pass the filename back in any case for manual display if
>you want that, but it still won't be submitted as part of them form.
>
>Regards,
>Malcolm

Thanks for the pointer (and the note that this is expected behavior).
--Ray

Reply all
Reply to author
Forward
0 new messages