Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 March 10, 2008 11:19:55

Paolo F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

File Upload with newforms


I've got a problem with file upload in django. I found some generic
documentation like this:http://www.djangoproject.com/documentation/model-api/#filefieldhttp://www.djangoproject.com/documentation/faq/#how-do-i-use-image-an...

Reading that pages, I suppose that the file uploading copy into my
MEDIA_ROOT happens automatically when I save my record.
The problem is that the file name is saved into my database table, but
the file isn't saved into the MEDIA_ROOT directory in the filesystem.
Where am I wrong? Here is the code (I use django SVN version under
Windows XP):

# models.py

class Auto(models.Model):

foto = models.ImageField(upload_to="images/auto/", blank=True,
null=True)

# forms.py

class AddImageForm(forms.Form):
immagine = forms.ImageField(label='Immagine:', required=True)

# views.py

def addphotoauto(request, id):
if request.method == 'GET':
form = AddImageForm()
return render_to_response('auto/aggiungi_foto.html', {'form':
form})
if request.method == 'POST':
form = AddImageForm(request.POST, request.FILES)
if form.is_valid():
try:
a = Auto.objects.get(id__exact = id)
a.foto = form.cleaned_data.get('immagine')
a.save()
# TODO: save file
return HttpResponseRedirect('/')
except Exception, e:
# exception handling
else:
return render_to_response('auto/aggiungi_foto.html',
{'form': form})
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 10, 2008 14:02:32

Michael N.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

File Upload with newforms


Everything looks right in your code, on a brief glance. In your
template, do you have your form prepared for multipart data; Does your
form tag look like <form enctype="multipart/form-data" method="post"
action="/foo/"> ?

Might be something else, but that is the first thing that jumps out to
me.

Michael

On Mar 10, 6:09 am, Paolo Ferretti <> wrote:
> I've got a problem with file upload in django. I found some generic
> documentation like this:
>
>http://www.djangoproject.com/documentation/model-api/#filefieldhttp://www.djangoproject.com/documentation/faq/#how-do-i-use-image-an...
>
> Reading that pages, I suppose that the file uploading copy into my
> MEDIA_ROOT happens automatically when I save my record.
> The problem is that the file name is saved into my database table, but
> the file isn't saved into the MEDIA_ROOT directory in the filesystem.
> Where am I wrong? Here is the code (I use django SVN version under
> Windows XP):
>
> # models.py
>
> class Auto(models.Model):
>
> foto = models.ImageField(upload_to="images/auto/", blank=True,
> null=True)
>
> # forms.py
>
> class AddImageForm(forms.Form):
> immagine = forms.ImageField(label='Immagine:', required=True)
>
> # views.py
>
> def addphotoauto(request, id):
> if request.method == 'GET':
> form = AddImageForm()
> return render_to_response('auto/aggiungi_foto.html', {'form':
> form})
> if request.method == 'POST':
> form = AddImageForm(request.POST, request.FILES)
> if form.is_valid():
> try:
> a = Auto.objects.get(id__exact = id)
> a.foto = form.cleaned_data.get('immagine')
> a.save()
> # TODO: save file
> return HttpResponseRedirect('/')
> except Exception, e:
> # exception handling
> else:
> return render_to_response('auto/aggiungi_foto.html',
> {'form': form})
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#3 March 10, 2008 14:33:04

Marty A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

File Upload with newforms


On Mon, Mar 10, 2008 at 6:09 AM, Paolo Ferretti
<> wrote:
> I've got a problem with file upload in django. I found some generic
> documentation like this:
>
>http://www.djangoproject.com/documentation/model-api/#filefield>http://www.djangoproject.com/documentation/faq/#how-do-i-use-image-an...

If you follow the link from the model-api page to the db-api page
you'll notice a method designed to do exactly what you need. Try
something like this in your views.py:

def addphotoauto(request, id):
if request.method == 'GET':
form = AddImageForm()
return render_to_response('auto/aggiungi_foto.html', {'form':
form})
if request.method == 'POST':
form = AddImageForm(request.POST, request.FILES)
if form.is_valid():
try:
a = Auto.objects.get(id__exact = id)
filename = form.cleaned_data.filename
content = form.cleaned_data.content
a.save_foto_file(filename, content)
return HttpResponseRedirect('/')
except Exception, e:
# exception handling
else:
return render_to_response('auto/aggiungi_foto.html',
{'form': form})

Note that you don't need to explicitly save the model after using
save_foto_file(), because it does that for you, by default. This whole
situation probably does need to be documented a bit better, as you're
far from the first person to miss that method.

-Gul

http://www.djangoproject.com/documentation/db-api/#get-foo-filenamehttp://www.djangoproject.com/documentation/db-api/#save-foo-file-filename-raw-contents--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

Board footer

Moderator control

Enjoy the 16th of December
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