Django CSRF 验证失败。请求中止。错误
Posted
技术标签:
【中文标题】Django CSRF 验证失败。请求中止。错误【英文标题】:Django CSRF verification failed. Request aborted. error 【发布时间】:2021-11-12 08:06:51 【问题描述】:我正在尝试使用 dropzone.js 上传图像,但它没有使用登录或注册。我得到CSRF verification failed. Request aborted.
Dropzone.options.fileDropzone =
url: '/media/',
init: function()
this.on("error", function(file, message)
alert(message);
this.removeFile(file);
);
var submitBtn = document.querySelector("#submitBtn");
var myDropzone = this;
submitBtn.addEventListener("click", function()
console.log("upload");
myDropzone.processQueue();
)
,
autoprocessQueue: true,
clickable: true,
thumbnailHeight: 90,
thumbnailWidth: 115,
maxFiles: 1,
maxFilesize: 10,
parallelUploads: 1,
addRemoveLinks: true,
dictRemoveFile: 'delete',
uploadMultiple: false,
<form class="imageUploaderForm" method="post" id="upload" enctype="multipart/form-data">
% csrf_token %
<div class="form-title">Upload your Image</div>
<div class="upload-type">File should be Jpeg, Png, Gif</div>
<div class="dropzone" tabindex="0" class="drop-zone" style="border-style: dotted;" id="fileDropzone">
<input accept="image/jpeg, image/png, image/gif" multiple="" type="file" autocomplete="off" tabindex="-1" name="myFile" class="drop-zone-input" style="display: none;">
<div class="placeholder">
<img class="hide" src="#" id="preview">
</div>
</div>
<span style="color: rgb(169, 169, 169);">or</span>
</form>
我用过
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
和
headers: 'x-csrf-token': CSRF_TOKEN, ,
这两个代码,但它仍然有同样的错误
【问题讨论】:
【参考方案1】:尝试使用以下方法:
headers:
'X-CSRF-TOKEN': $("input[name=csrfmiddlewaretoken]").val()
【讨论】:
以上是关于Django CSRF 验证失败。请求中止。错误的主要内容,如果未能解决你的问题,请参考以下文章