我在这里试过
我的forms.py文件
class UploadfileForm(forms.Form): file = forms.fileFIEld(label='Select a file',help_text='max. 42 megabytes')
我的vIEws.py
import xlrd from property.forms import UploadfileFormdef excel(request): if request.method == 'POST': form = UploadfileForm(request.POST,request.fileS) if form.is_valID(): newdoc = handle_uploaded_file(request.fileS['file']) print newdoc print "you in" newdoc.save() return httpResponseRedirect(reverse('upload.vIEws.excel')) else: form = UploadfileForm() # A empty,unbound form #documents = document.objects.all() return render_to_response('property/List.HTML',{'form': form},context_instance=RequestContext(request))def handle_uploaded_file(f): destination = open('media/file/sheet.xls','wb+') for chunk in f.chunks(): destination.write(chunk) destination.close()
所以在尝试这个时我得到了错误.
IOError at /property/excel/[Errno 2] No such file or directory: 'media/file/sheet.xls'Request Method: POSTRequest URL: http://127.0.0.1:8000/property/excel/Django Version: 1.5Exception Type: IOErrorException Value: [Errno 2] No such file or directory: 'media/file/sheet.xls'Exception Location: D:\Django_workspacethmarch\dtz\property\vIEws.py in handle_uploaded_file,line 785
请帮我解决这个问题,handle_uploaded_file()函数有问题.
解决方法 如果你使用open(如open(‘path’,’wb’),那么你需要使用FulL路径.你能做的是:
from django.conf import settingsdestination = open(settings.MEDIA_ROOT + filename,'wb+')总结
以上是内存溢出为你收集整理的python – 如何在不创建模型的情况下在django中保存文件全部内容,希望文章能够帮你解决python – 如何在不创建模型的情况下在django中保存文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)