python – Django ImageField: files dont get uploaded – Education Career Blog

I implemented some ImageFields in my model and installed PIL (not the cleanest install). Things seem to work as I get an upload button in the admin and when I call the .url property in the view I get the string with the filename + its upload property.
The problem is that the file is not there, apparently it doesnt get uploaded once I save the model.

Any idea?


Here’s a sample of my code situation

class My_Model(models.Model):
     image = models.ImageField(upload_to = 'images/my_models/main')

'image': query.my_model.image.url




Make sure that you’re binding request.FILES to the form when POSTing, and that the form is declared as multi-part in the template

Here’s the view from one of my applications:

def submit(request):
    if request.method == 'POST':
        (Photo.objects.count()+1, request.FILES'photo'.name.split(".")1), request.FILES'photo')}
        form = PhotoForm(request.POST, request.FILES)
        if form.is_valid():
            new = Photo(photo=request.FILES'photo', name=request.POST'name')
            return HttpResponseRedirect('/') # Redirect after POST
        form = PhotoForm()
    return render_to_response('app/submit.html', {'form': form}, context_instance=RequestContext(request))

and the PhotoForm class:

class PhotoForm(forms.ModelForm):
    class Meta:
        model = Photo
        fields = ('name', 'photo')

Leave a Comment