3

I am writing my first django app that uses the ImageField and I am having difficulty. The problem is that my images are not uploading. I have looked at as many examples that I can find. And I'm not sure what's going wrong here.

I am trying to verify that my photos are uploading by looking in the location of the upload_to directory. When the form is displayed in the web page the correct upload file button is displayed. When I hit submit, the code below executes, but no images are ever uploaded.

Based on the upload_to, I am expecting to see images uploaded to see images under either: myproject/photos or myproject/media/photos correct?

Am I doing anything obvious wrong here? How do I get my images to upload?

--------------settings.py-------------

MEDIA_ROOT = '/home/me/django_projects/myproject/media/'
MEDIA_URL = '/media/'

--------------model.py-------------

class Person(models.Model):
    lastName = models.CharField(max_length=20)
    firstName = models.CharField(max_length=20)
    image = models.ImageField(upload_to='photos', blank=True, null=True)

    #  save executes but no image is saved.
    #  Because images are uploaded along with a new entry, I needed a special
    #  work around to get the self.id
    def save(self):
        for field in self._meta.fields:
            if field.name == 'image':
               if self.id is not None:
                 #save image now
                 field.upload_to = 'photos/%d' % self.id
               else:
                 #Save image later
                 saveImageLast = True
        super(Customer, self).save()  # should create self.id if not already
        if saveImageLast == True:
            field.upload_to = 'photos/%d' % self.id
            super(Customer, self).save()
            print "save complete"  #prints, but no image ...?

--------------forms.py-------------

class PersonForm(ModelForm):

  class Meta:
      model = Person
      fields = ( 'lastName', 'firstName', 'image' )
1

3 Answers 3

13

from django documentation, i think this can help (in the past this helped me):

Firstly, in order to upload files, you'll need to make sure that your element correctly defines the enctype as "multipart/form-data"

<form enctype="multipart/form-data" method="post" action="/foo/">
Sign up to request clarification or add additional context in comments.

1 Comment

I made sure that my form includes the enctype as "multipart/form-data". But still no upload. :(
5

In your view where you create an instance of the form with post data, ensure you have provided request.FILES

form = PersonForm(request.POST, request.FILES)

Comments

1

This is a bit late, but 'upload_to' is not a method. It's an attribute that represents the relative path from your MEDIA_ROOT. If you want to save an image in the folder 'photos' with the filename self.id, you need to create a function at the top of your model class. For instance:

class Person(models.Model):
    def file_path(instance):
        return '/'.join(['photos', instance.id])
    image = models.ImageField(upload_to=file_path)

Then when you are actually saving your image you would call:

person = Person(firstName='hey', lasteName='joe')
person.image.save(ImageObject.name, ImageObject)

More on the image file objects here.

More on upload_to here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.