Trying to upload an image to a model image
field with AJAX. Here's my code:
js
$('input#id_banner_image').on('change', function(e) {
$.ajax({
type: 'POST',
url: '/change_banner_image/',
data: {
image: URL.createObjectURL(e.target.files[0]),
csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val()
}
})
});
views
def change_banner_image(request):
if request.is_ajax():
print(request.FILES) #prints <MultiValueDict: {}>
for i in request.FILES:
print(i) #prints nothing
image = request.FILES['id_banner_image'].name #this is wrong
profile = get_object_or_404(Profile, user=request.user)
profile.image = image
profile.save()
return HttpResponse()
How do I correctly grab the image in my views
? I know that's the bit that's wrong.