【问题标题】:Nonetype error while saving file with dropzone js in Django在Django中使用dropzone js保存文件时出现Nonetype错误
【发布时间】:2021-09-15 19:39:37
【问题描述】:

我正在尝试使用 dropzone.js 将文档/图像保存到我的 SQL 数据库中,用于使用 Django 制作的网络应用程序

HTML 文件:

{% extends 'Main/logged_base_expert.html' %}
{% block content %}
{% load static %}
    
<head>
  <script src="https://rawgit.com/enyo/dropzone/master/dist/dropzone.js"></script>
<link rel="stylesheet" href="https://rawgit.com/enyo/dropzone/master/dist/dropzone.css">
</head>
<body>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.5/css/bootstrap-select.min.css" media="screen">

<form action="{% url 'document_upload' %}" method = "POST" class="dropzone dz" role="form" enctype="multipart/form-data">
  {% csrf_token %}
  <div class="fallback">
    <input name="file" type="file" id="file" multiple >
  </div>
  <button type="submit" class="form-control" id="cf-submit" name="submit">Submit</button>
</form>


  <!-- client section end -->
    <!-- footer section start -->
{% endblock %}

views.py:

def document_upload(request):
    c = main.objects.get(username = request.session['username'])
    unq_id = c.unq_id
    print("overall")
    if request.method == 'POST':
        images = request.FILES.get('file')
        print("image")
        fs = FileSystemStorage()
        filename = fs.save(images.name, images)
        Doc1 = Doc(user_id = unq_id, upload = images)
        Doc1.save()

    return render(request, 'Main/document_upload.html')

models.py:

class Doc(models.Model):
    unq_id  = models.AutoField(primary_key=True)
    user_id = models.BigIntegerField()
    upload  = models.ImageField(upload_to= 'expert/images')

    def __str__(self):
        return str(self.unq_id)

我一直面临的问题是,我一直收到错误

AttributeError at /document_upload/
'NoneType' object has no attribute 'name'

它出现在views.py中的以下行中:

filename = fs.save(images.name, images)

附加信息:图像的相对路径似乎完美地存储在数据库中,但由于我不断收到错误,我无法将应用程序重定向到另一个页面

【问题讨论】:

    标签: python django dropzone.js


    【解决方案1】:

    您收到 AttributeError 是因为在您的请求中找不到它 - request.FILES.get('file')。打印 request.FILES 看看里面有什么。

    【讨论】:

    • ]}> 它正在打印两次,第二次没有任何名称
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    相关资源
    最近更新 更多