【问题标题】:Multiple File Upload Code for DjangoDjango的多文件上传代码
【发布时间】:2015-08-08 03:57:47
【问题描述】:

好的。因此,在 Stackoverflow 中搜索了很多论坛和其他帖子后,我想出了以下代码。首先,我尝试使用request.FILES.get('onefile'),但无论我在上传过程中选择了多少个文件,它都只上传了一个文件。因此,我对代码进行了一些更改,以上传名称为 file1、file2 等的文件。以下是我的代码,但我不知道为什么这不起作用。当前没有文件正在上传,它显示以下代码下列出的错误。

upload2.html

<!DOCTYPE html>

<html>

<head>
</head>

<body>
    <form method="post" action="/index/multi/" enctype="multipart/form-data"> {% csrf_token %}
        <input type="file" name="onefile" multiple>

        <input type="submit" value="Upload">
    </form>
</body>

</html>

views.py

from django.shortcuts import render
from django.http import HttpResponse

def upload2(request):
    return render(request, "upload2.html", {})

def multi(request):
    count = 1
    for x in request.FILES.getlist('onefile'):
        print request.FILES.getlist('onefile')
        def handle_uploaded_file(f):
            with open('/home/michel/django/upload/media/file' + count, 'wb+') as destination:
                for chunk in f.chunks():
                    destination.write(chunk)
        handle_uploaded_file(x)
        count = count + 1
    return HttpResponse('Uploaded!')

urls.py

from django.conf.urls import url

from index import views

urlpatterns = [
    url(r'^upload2/$', views.upload2),
    url(r'^multi/$', views.multi),
]

错误

TypeError at /index/multi/
cannot concatenate 'str' and 'int' objects
Request Method: POST
Request URL:    http://localhost:8000/index/multi/
Django Version: 1.8.1
Exception Type: TypeError
Exception Value:    
cannot concatenate 'str' and 'int' objects
Exception Location: /home/michel/django/upload/index/views.py in handle_uploaded_file, line 32

我不确定我是否错过了什么。如果您需要其他任何东西,请告诉我。提前致谢!

【问题讨论】:

    标签: python django file upload


    【解决方案1】:

    我不确定我是否遗漏了什么。

    是的:阅读错误消息——告诉你错误是什么——回溯——告诉你错误发生在哪里——然后重新阅读你的代码。所以你有:

    异常类型:TypeError 异常值:
    不能连接 'str' 和 'int' 对象

    这意味着您尝试连接一个字符串和一个整数(Python 不允许这样做,原因很明显)。

    异常位置:/home/michel/django/upload/index/views.py in handle_uploaded_file, line 32

    这意味着错误在 /home/michel/django/upload/index/views.py 的第 32 行。

    /home/michel/django/upload/index/views.py 的第 32 行是:

    with open('/home/michel/django/upload/media/file' + count, 'wb+') as destination:
    

    显然,'/home/michel/django/upload/media/file' + count 是罪魁祸首。现在你只需要解决这个问题,要么制作一个count 的字符串,要么使用字符串格式——这两者都在 FineManual(tm) 中进行了解释。

    在阅读过程中,您可能还想了解内置的enumate(sequence) 函数。

    【讨论】:

    • 我觉得自己很笨。感谢您抽出宝贵的时间回答这么多细节。我认为我需要花更多时间在 Python 而不是 Django 上。我不知道 Python 中的很多函数。我按照@chandu 的建议添加了 str() 并且成功了。
    • 字符串格式化是 Python 中的首选解决方案 - 如果只是为了可读性。
    【解决方案2】:

    它连接错误,你不能连接'str'和'int'对象。

    我认为问题出在这一行

    '/home/michel/django/upload/media/file' + count
    

    解决方案

    '/home/michel/django/upload/media/file' + str(count)
    

    例子:

    a = 'test'
    b = 1
    c = a+b
    Type Error: cannot concatenate 'str' and 'int' objects
    

    解决方案:

    a = 'test'
    b = str(1) or '1'
    c = a+b(works fine)
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      相关资源
      最近更新 更多