I'm new to Django and I've been struggling with getting ImageField to
upload images.

I have a very simple model that I want to use to store images:

class WriteupImage(models.Model):
    writeup=models.ForeignKey(Writeup)
    image = models.ImageField(upload_to="writeup/photos/")

I have a form with an ImageField:

class WriteupAddForm(forms.Form):
    ... other fields that all submit fine, plus ...
    photo = forms.ImageField(required=False)

When I submit the form, the view is able to bind the form OK and there
is a 'photo' entry in the form's self.files dictionary.

My problem occurs when I try to save the form.  I'm not sure how to
properly create the WriteupImage instance and save it.  Here is the
code I'm using within the form's 'save' method:

        if 'photo' in self.files:
            self.log('found photo in self.files')
            photo = self.files['photo']
        else:
            logging.debug('photo missing from self.files')
            ... skip the rest of the stuff below if there are no
images in self.files (code omitted) ...
        logging.debug('instantiating WriteupImage')
        image = WriteupImage(writeup=self.writeup)
        logging.debug('saving image data')
        try:
            image.image.save('w'+str(self.writeup.id)
+'image.jpg',photo)
        except Exception, e:
            logging.debug('problem saving image data: ' + str(e))
            raise e
        try:
            self.log('saving image object')
            image.save()
        except Exception, e:
            self.log('got exception saving the WriteupImage: ' +
str(e))
            raise e

The line "image.image.save('w'+str(self.writeup.id)
+'image.jpg',photo)" always raises an exception with the error
message: "[Errno 45] Operation not supported ".  A file with the
requested name is created in the appropriate media directory but it
has zero length (i.e. it contains no image data).

Can someone point out what I'm doing wrong?  I've been struggling with
this for quite awhile now.

Initially I was using the development server started by "./manage.py
runserver".  I thought the development server might have problems
uploading image files, so I deployed the app on apache with mod_wsgi.
Unfortunately, I get the same error message when attempting to upload
an image to the apache server.

Both the client and server are running on Mac OSX Snow Leopard.  I've
tried using Firefox and Safari on the client side: both yield the same
behaviour and exception so I'm pretty sure the problem is with how I'm
trying to instantiate and save the WriteupImage instance.

Thanks in advance for any help.

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