Form attachment (FileField) upload not saving.

318 views
Skip to first unread message

Sithembewena Lloyd Dube

unread,
Dec 8, 2010, 5:02:04 AM12/8/10
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

Sithembewena Lloyd Dube

unread,
Dec 8, 2010, 5:34:35 AM12/8/10
to django...@googlegroups.com
50% solved - I passed request.FILES into the form's constructor as indicated below:

http://docs.djangoproject.com/en/1.2/topics/http/file-uploads/

I then accessed the file as follows:

attachment = request.FILES['attachment']


Only problem left is to figure out why the form is posting twice. ideas please?

Thanks.

Sithembewena Lloyd Dube

unread,
Dec 8, 2010, 6:49:18 AM12/8/10
to django...@googlegroups.com
100% fixed. I was calling save() on the form twice, once in form_capture and once in form_errors.

:(
Reply all
Reply to author
Forward
0 new messages