【问题标题】:Django CSRF verification failed. Request aborted. errorDjango CSRF 验证失败。请求中止。错误
【发布时间】: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" width="115" height="90" 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, },

这两个代码,但它仍然有同样的错误

【问题讨论】:

    标签: javascript django dropzone.js django-csrf


    【解决方案1】:

    尝试使用以下方法:

    headers: {
            'X-CSRF-TOKEN': $("input[name=csrfmiddlewaretoken]").val()
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-03
      • 2012-12-07
      • 2015-03-08
      • 1970-01-01
      • 2015-02-28
      • 2014-02-03
      • 2016-05-29
      • 1970-01-01
      相关资源
      最近更新 更多