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'.