尝试使用AJAX将图像上传到模型image
字段。这是我的代码:
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()
}
})
});
视图
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()
如何正确抓取views
中的图片?我知道这有点不对。