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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---