ImageField doesn't upload
-
05-06-2021 - |
Question
I have been trying to implement a image upload for changing avatar for individual users. The problem I have right now is that it never uploads to the folder. It works from the admin but it doesn't work on the template I've created
views.py
if 'avatar_upload' in request.POST:
avatar_form = UserAvatarForm(request.POST, request.FILES, instance=request.user.get_profile())
if avatar_form.is_valid():
avatar_form.save()
return HttpResponse(request.POST['avatar'])
return HttpResponse("Failed")
I've changed the code for viewing the outputs. I get the file name in the POST
. But I don't have anything in the request.FILES
. So I'm guessing it is a problem there, and I haven't found out so far what the problem could be. Or could it be some problem elsewhere?
template*
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% for item in avatar_form %}<p>{{ item.label }}: {{ item }}</p>
{% endfor %}
<input type="submit" value="Upload avatar" name="avatar_upload">
</form>
Solution
Have you set enctype="multipart/form-data"
in form in the template?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow