django文件批量上传-简写版

Posted on 2016-08-15 17:00  沉睡的码农  阅读(296)  评论(0)    收藏  举报

模板中创建表单

<form method='post' enctype='multipart/form-data' action='/upload/'>
    <input type='file' name='upload_img'/>
    <input type='file' name='upload_img'/>    
    <input type='submit' value='submit'>
</form>

在urls中到处一个upload地址

 url(r'^upload/$',upload),

最后写控制器

@csrf_exempt
def upload(req):
    if req.method=='POST':
        files=req.FILES.getlist('upload_img')
        for f in files:
            print f.name
            with open('/...url.../'+f.name,'wb+') as des:
                for chunk in f.chunks():
                    des.write(chunk)
    return render_to_response('index.html')

完毕