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
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/
Thanks for the pointer (and the note that this is expected behavior).
--Ray