python - Image is not uploading via form -
i making django project , have 1 problem uploading images in forms.
so, tried add 1 object image, in admin working, in form on site - not.
my views:
def newbook(request, user_id): form = bookadd(request.post or none) if form.is_valid(): book = form.save(commit=false) book.author = get_object_or_404(user, pk=user_id) book.save() return redirect('../%s' % book.id) return render(request, 'userbook/newbook.html', {'form': form})
my model:
class book(models.model): """book compilation of sections subjects.""" author = models.foreignkey(auth_user_model) name = models.charfield(max_length=100) description = models.charfield(blank=true, max_length=256) cover = models.imagefield(upload_to='img/bookcovers') def __str__(self): return self.name
my form:
class bookadd(modelform): class meta: model = book fields = ('name', 'description', 'cover')
when add new book, error "the field required", maybe field of cover, image added. work on local server, don't work on pythonanywhere.com
you have change code
form = bookadd(request.post or none)
to
form = bookadd(request.post,request.files)
and form should have enctype="multipart/form-data"
<form action="." method="post" enctype="multipart/form-data">
Comments
Post a Comment