views:

31

answers:

1

Hello, 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?

Thanks

Here's a sample of my code situation

models.py

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

view.py

'image': query.my_model.image.url

result:

static/images/my_models/main/theimage.png
+1  A: 

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:

@login_required
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'])
            new.save()          
            return HttpResponseRedirect('/') # Redirect after POST
    else:
        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')
Nick Barnwell
mmmh I am not very familiar with your method... you dont use ImageField aren't you? When I look in the folder on the server there's no image uploaded...
mαττjαĸøb
@mαττjαĸøb - I guess you are talking about uploading through the admin?
lazerscience
yes. I solved it! the problem was an incorrect setting of MEDIA_ROOT and MEDIA_URL.
mαττjαĸøb