0

I'm trying to get uploading working with Django. I don't get an error when I try to upload it, and the new Item is saved, but there is no image. The other form data is all there. I've been googling and on stack overflow for the past 2 days and cannot find a fix. I don't even know where it's going wrong! Any advice would be amazing. Thanks so much. :)

from views.py:

    if request.method == 'POST':
        form = ItemForm(request.POST, request.FILES, user=request.user, forsale=True)
        if form.is_valid():
            new_item = form.save()
            return item_view(request, marketplace_id, new_item.id)
    else:
        form = ItemForm(user=request.user)

from models.py:

class Item(models.Model):

    photo =  StdImageField(upload_to='images/', blank=True, size=(500, 500, True), thumbnail_size=(210, 150, True))

    user = models.ForeignKey(User)
    ...

    def __unicode__(self):
        return self.name

class ItemForm(ModelForm):
    class Meta:
        model = Item
        fields = ['name', 'description', 'price', 'shipping', 'photo', 'section']

    def __init__(self, *args, **kwargs):
        self._user = kwargs.pop('user')
        super(ItemForm, self).__init__(*args, **kwargs)

from the template:

    <form enctype="multipart/form-data" action="" method="post" data-ajax="false">{% csrf_token %}
            {% for field in form %}
                <div class="fieldWrapper" data-role="fieldcontain">
                    {{ field.errors }}
                    {{ field.label_tag }} {{ field }}
                </div>
            {% endfor %}

        <input type="submit" value="Save">
    </form> 

As I see it, the template is correct. The Item model seems fine, StdImageField is a wrapper for ImageField. It works fine in the admin panel. The Form seems fine as well, nothing special. In my views.py fine, I check for a POST request, call the ItemForm and pass in request.POST and request.FILES (and forsale which is a boolean). I check if the form is valid, if so, I save it. Why isn't it working? The Item saves but in the admin panel there is no file associated with 'photo'.

  • I would suggest debugging :) Also it may help to look at generated form html, view coming `request.FILES`. Is `StdImageField` class is correct, will it work with standard `ImageField`? Also it's a bad idea to call another view inside other view. There should be a redirect instead. – demalexx Dec 28 '11 at 13:34

1 Answers1

0

Check that MEDIA_ROOT setting joined with "images/" is a valid and writable path for Django process.

Also using pdb

import pdb; pdb.set_trace()

can help you in the process of finding the issue, (f.i. have a look at request.FILES to see if everything looks ok there during post).

Tommaso Barbugli
  • 11,781
  • 2
  • 42
  • 41