如何解决django文件上传错误'image'属性没有关联的文件
我正在使用dropzone.js将文件上传到Django。使用文件上传区后,由于未上传文件,我收到错误消息。
ValueError'image'属性没有与之关联的文件。
我的Django View函数如下:
@csrf_exempt
def project_image_alternative_form_submit_ajax(request,object_id):
project_image = ProjectImage.objects.filter(pk=object_id).first()
if not project_image:
response_json = {
'message': 'Image you provided pk for doesnt exist!',}
return JsonResponse(response_json,status=status.HTTP_400_BAD_REQUEST)
if request.method == 'POST':
if request.is_ajax():
image_file = request.FILES.get('image_file')
project_image_alternative = ProjectImageAlternative(
project_image=project_image,image=image_file,)
project_image_alternative.save()
response_json = {
'message': 'Alternative Image saved successfully!','image_alternative_pk': project_image_alternative.pk
}
return JsonResponse(response_json,status=status.HTTP_200_OK)
else:
response_json = {
'message': 'Please send a POST request',status=status.HTTP_400_BAD_REQUEST)
return JsonResponse(response_json,status=status.HTTP_200_OK)
我的Dropzone JS函数如下:
$('.alternative_image_upload').dropzone({
maxFiles: 1,autoProcessQueue: true,parallelUploads: 1,headers: {
'Cache-Control': null,'X-Requested-With': null,},error: function(file,response,xhr) {
if (typeof xhr !== 'undefined') {
this.defaultOptions.error(file,xhr.statusText);// use xhr err (from server)
} else {
this.defaultOptions.error(file,response);// use default (from dropzone)
}
},sending: function(file,xhr,formData) {
var pk = this.element.dataset['pk'];
formData.append('pk',pk);
},init: function() {
this.on("success",function(file,response) {
console.log(response);
});
},uploadMultiple: false,acceptedFiles: '.jpg,.jpeg,.png,.svg'
})
我可以得到有关解决方案的任何建议吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。