upload multiple files in django

前端 未结 4 1940
忘了有多久
忘了有多久 2020-12-18 09:33

I am new to django, I am trying to upload more than one file from the browser and store them somewhere in computer storage but I am not storing them successfully with this c

4条回答
  •  死守一世寂寞
    2020-12-18 10:25

    views.py

    from django.shortcuts import render
    from django.http import HttpResponse
    # Create your views here.
    
    def Form(request):
        for x in request.FILES.getlist("files"):
            def process(f):
                with open('/Users/benq/djangogirls/upload/media/file_' + str(x), 'wb+') as destination:
                    for chunk in f.chunks():
                        destination.write(chunk) 
            process(x)
        return render(request, "index/form.html", {})
    

    urls.py

    from django.conf.urls import url
    from index import views
    
    urlpatterns = [
        url('form', views.Form),
    ]
    

    worked for me.

提交回复
热议问题