Monday, May 30, 2016

ImageField | error

Folks,

I'm using django 1.9. I'm having the following problem. When I update the object of Pessoa, described below, I receive the following error message: "i/o operation on closed file". Does anyone have any solution. Thanks.

Erick


**** models.py ****

class Pessoa(models.Model):
        ...
imagem = models.ImageField(upload_to=upload_location, blank=True, null=True)
        ...


**** views.py

def image_update(request, pessoa_id):
instance = get_object_or_404(Pessoa, id=pessoa_id)
form = ImageForm(request.POST or None, request.FILES or None, instance=instance)
if(form.is_valid()):
if(instance.imagem != None):
instance.imagem.delete()
instance.imagem = request.FILES.get('imagem')
instance.last_update_at = date.today()
instance.save()
#return HttpResponseRedirect(reverse('pessoa:editar', kwargs={'pessoa_id': pessoa_id}))
context = {
"instance": instance,
"form": form
}
return render(request, "pessoa/pessoa_image_form.html", context)


**** forms.py

class ImageForm(forms.ModelForm):
class Meta:
model = Pessoa
fields = ['imagem']


**** html
<form id="formulario" action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.field_errors }}
        <div class="fieldWrapper">
{{ form.imagem.errors }}
<label for="nome">Imagem:</label><br/>
{{ form.imagem }}
</div><br/>
<input type="submit" value="Salvar" class="button">
</form>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALUDr%2B0Uvre-ahXcVBe8Ms-Z0_KVhLJ_%3DvuA%2BLizfdy9zfjuAg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment