django upload files proving more difficult than necessary

不打扰是莪最后的温柔 提交于 2019-12-11 11:57:45

问题


I am trying to allow for files to be uploaded by users in on my django site. I started with the example command from the django documentation, input into views.py, independently of a form or model and just referred to in the template (and modified it so that multiple files can be uploaded at once,):

  def Upload(request):
    for count, x in enumerate(request.FILES.getlist("files")):# allows for multiple iterations/files
      def process():
         with open('/Users/Deirdre/bing/upload/media/file_', + str(count) 'wb+') as destination:
            for chunk in f.chunks():
               destination.write(chunk)
       process(x)
     return HttpResponse("File(s) uploaded")

However on the "with open... as" the server keeps returning the errors "SyntaxError: invalid syntax" or "unexpected indentation".... I know that neither of these are true so is there a way to bypass this difficulty? why is django not configuring with the commands???


回答1:


Your indentation is wrong! The correct indentation is give below, there has to be 4 space indent

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

def Upload(request):
    for count, x in enumerate(request.FILES.getlist("files")):
        def process(f):
            with open('/Users/Michel/django_1.8/projects/upload/media/file_' + str(count), 'wb+') as destination:
                for chunk in f.chunks():
                    destination.write(chunk)
        process(x)
    return HttpResponse("File(s) uploaded!")


来源:https://stackoverflow.com/questions/35369180/django-upload-files-proving-more-difficult-than-necessary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!