How to save an imagefield in Django views? Not a modelform

Im trying to submit a form with some textfields and an imagefield. And save it as a new model object. Cant get it to work so im going over each part from the start now..

How do i handle (just saving it) the imagefield in my views? Do i need to write an extra handler? It's not a modelform but a regular form. Where does PIL/PIllow come into this. Should I import it? Can i clean the imagefield?

I've been trying this:

def UploadThis(request):

    if request.method == 'POST':
        form = TestForm(request.POST, request.FILES) 
        response = {}

            if form.is_valid():
                response["status"] = "OK"

                justtesting = Newmodel()

                cd = form.cleaned_data

                justtesting.name = cd['name']
                justtesting.type = cd['type']
                justtesting.description = cd['description']
                justtesting.picture = request.FILES['picture']
                justtesting.save()

Model:

class Newmodel(models.Model):
    name = models.CharField(max_length=50)
    type = models.CharField(max_length=50)
    description = models.CharField(max_length=140, blank=True)
    picture = models.ImageField(upload_to='images/testfiles', blank=True)

    def __unicode__(self):
        return self.name

Form:

class TestForm(forms.Form):

    name = forms.CharField(max_length=50)
    type = forms.CharField(max_length=50)
    description = forms.CharField(max_length=140)
    picture = forms.ImageField()


    def clean(self):
        cleaned_data = self.cleaned_data

        name = cleaned_data.get("name")
        description = cleaned_data.get("description")
        type = cleaned_data.get("type")

        return cleaned_data

Answers


Your error is that you are trying to assign the image to the model field using request.POST, when you actually need to use form.cleaned_data like so:

justtesting.picture = form.cleaned_data['picture']

Your clean method on the form is not doing anything currently. You could do validation (although that is not neccessary) by using a clean method something like this:

def clean_image(self):
    image = self.cleaned_data.get['image']
    if image:
        # do some validation, if it fails
        raise forms.ValidationError(u'Form error')
    return image

For an example that validates the image size see this question: https://stackoverflow.com/a/16425455/1744645

Using PIL/Pillow is not necessary and only required if you want to some form of image processing. You could have a save method on your model, for example to resize the image:

def save(self,force_insert=False, force_update=False, *args, **kwargs):

    # save everything else
    super(Newmodel, self).save(force_insert, force_update)

    if self.image:
         if self.image.width > 300 or self.image.height > 300:
             resize_image(self.image)

PIL/Pillow itself has the following basic operations that might be of interest:

img = PIL.open(PATH_TO_IMAGE)
img = img.resize((x, y), PIL.ANTIALIAS)
img = img.crop((0, 0, x, y))
img.save(path,quality=90)

But see http://effbot.org/imagingbook/pil-index.htm for the full docs.


Need Your Help

hibernate.hbm2ddl.import_files: Path to the files

java sql spring hibernate import

I need that hibernate read a sql file and do whatever it needs to do during the instantiation of SessionFactory, so I did the following configuration:

Can I play more than one file as a ringtone in android app?

android ringtone

I want to play couple of files as one ringtone in android app, How can I do it?