问题
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