Issue
I will create multi image add page. But the following error occurs. Could your help me? I think mistake in views.py. I'm not sure.
Note : It's frustrating that Stackoverflow asks for a long explanation.
error:
AttributeError at /en/projects/multiimageadd/10/
'bytes' object has no attribute '_committed'
@login_required
@permission_required('is_superuser')
def MultiImageAdd(request, id):
blog = BlogModel.objects.filter(id=id).first()
if request.method == 'POST':
images = request.FILES['images']
for image in images:
Images.objects.create(project=project, image=image)
return redirect("home")
return render(request,"multiImage.html")
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header">
</div>
<div class="card-body">
<form class="needs-validation" method="POST" action="" enctype="multipart/form-data"
novalidate="">
{% csrf_token %}
<div class="row">
<div class="col-sm-12">
<div class="form-group row">
<label for="id_images" class="col-xl-3 col-md-4">Images:</label>
<span class="form-control col-md-8">
<input required type="file" name="images" id="id_images" multiple>
</span>
</div>
</div>
</div>
<div class="pull-right">
<button class="btn btn-primary" type="submit">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
models.py
class Images(models.Model):
blog = models.ForeignKey(BlogModel, related_name='blogmodel', on_delete=models.CASCADE, blank=True,null=True)
image = models.ImageField(blank=False, null=True)
Solution
You should enumerate over the file handlers, so obtain the handlers with .getlist(…)
[Django-doc]:
images = request.FILES.getlist('images')
for image in images:
Images.objects.create(project=project, image=image)
Answered By - Willem Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.