Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Form attachment (FileField) upload not saving. [RSS Feed]

#1 Dec. 8, 2010 10:02:15

Sithembewena L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form attachment (FileField) upload not saving.


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
attachment = form.cleaned_data
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 + '...'

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

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 8, 2010 10:34:44

Sithembewena L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form attachment (FileField) upload not saving.


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


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
> attachment = form.cleaned_data
> 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 + '...'
>
> 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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 8, 2010 11:49:29

Sithembewena L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form attachment (FileField) upload not saving.


100% fixed. I was calling save() on the form twice, once in form_capture and
once in form_errors.

:(



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

> 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
>
>
> 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
>> attachment = form.cleaned_data
>> 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 + '...'
>>
>> 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
>



--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Form attachment (FileField) upload not saving. [RSS Feed]

Board footer

Moderator control

Enjoy the 17th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support