django上传文件证明比必要的困难 [英] django upload files proving more difficult than necessary
问题描述
我正在尝试允许django网站上的用户上传文件.我从django文档中的example命令开始,输入到views.py,独立于表单或模型,仅在模板中引用(并对其进行了修改,以便可以一次上传多个文件):
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")
但是,在打开时为..."服务器上,服务器不断返回错误"SyntaxError:无效语法"或意外缩进". 我知道这些都不是真的,所以有办法绕过这个困难吗? django为什么不使用命令进行配置??
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???
推荐答案
您的缩进错误!正确的缩进如下所示,必须有4个空格缩进
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!")
这篇关于django上传文件证明比必要的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!