Sithembewena Lloyd Dube
unread,Dec 8, 2010, 5:02:04 AM12/8/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
Hi all,
I have a contact form that is supposed to save a message as well as a file upload. When I use the admin site, I can save a contact record with a file upload of any type.
However, saving from the 'front end' of my web application causes some strange behaviour - the file is not uploaded, and the contact record is saved twice as seen from the admin site.
My code is as follows:
----------------------------------------------------------------------------------------------------------------------------BEGIN CODE-------------------------------------------------------------------------------------------------------------------------------------------
html template:
<form action=".#contact-form" method="post" enctype="multipart/form-data">{% csrf_token %}
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="right" valign="top"><label>Message</label></td>
<td>{{ form.message }}</td>
</tr>
<tr>
<td align="right" valign="top"><label>Upload File:</label></td>
<td>{{ form.attachment }}</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit Request" /></td>
</tr>
</table>
</form>
view:
@csrf_protect
def contact_us(request):
form = form_capture(request)
return render_to_response('front_end/standard_pages/contactus.html', {'form': form, }, context_instance=RequestContext(request))
the form_capture function (extra module named functions.py - keeps all my custom functions):
def form_capture(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
message = form.cleaned_data['message']
attachment = form.cleaned_data['attachment']
form.save()
else:
form = ContactForm()
return form
the form (forms.py):
class ContactForm(ModelForm):
class Meta:
model = Contact
message = forms.CharField(widget=forms.Textarea(attrs={'cols': 60, 'rows': 10, 'class': 'mceNoEditor'}))
attachment = forms.FileField(required=False, widget=forms.FileInput())
the Contact model (models.py):
class Contact(models.Model):
message = models.TextField(max_length=500)
attachment = models.FileField(upload_to='attachments', blank=True)
def __unicode__(self):
return self.message[:10] + '...'
class Meta:
verbose_name_plural = "Contact Messages"
----------------------------------------------------------------------------------------------------------------------------END CODE-------------------------------------------------------------------------------------------------------------------------------------------
Any ideas why saving a record from the contact form posts twice, and why the file attachment is not uploaded?
Thanks!
--
Regards,
Sithembewena Lloyd Dube