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.

On Wed, Dec 8, 2010 at 12:02 PM, Sithembewena Lloyd Dube
<zebr...@gmail.com>wrote:

> 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
>



-- 
Regards,
Sithembewena Lloyd Dube

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to