
def upload(request): if request.POST: form = fileForm(request.POST,request.fileS) if form.is_valID(): form.save() return render_to_response('project/upload_successful.HTML') else: form = fileForm() args = {} args.update(csrf(request)) args['form'] = form return render_to_response('project/create.HTML',args) 然后,文档将与列表中的任何其他文档一起显示在列表中,您可以单击该文档,它将显示有关它们的基本信息以及它们已上载的文件的名称.从这里我希望能够使用以下链接再次下载相同的excel文件:
<a href="/project/download"> Download document </a>
我的网址是
urlpatterns = [ url(r'^$',ListVIEw.as_vIEw(queryset=Post.objects.all().order_by("-date")[:25],template_name="project/project.HTML")),url(r'^(?P<pk>\d+)$',DetailVIEw.as_vIEw(model=Post,template_name="project/post.HTML")),url(r'^upload/$',upload),url(r'^download/(?P<path>.*)$',serve,{'document root': settings.MEDIA_ROOT}),] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT) 但是我得到了错误,serve()得到了一个意外的关键字参数’document root’.谁能解释如何解决这个问题?
要么
说明如何使用上传的文件进行选择和提供
def download(request): file_name = #get the filename of desired excel file path_to_file = #get the path of desired excel file response = httpResponse(mimetype='application/force-download') response['Content-disposition'] = 'attachment; filename=%s' % smart_str(file_name) response['X-Sendfile'] = smart_str(path_to_file) return response解决方法 您在参数document_root中错过了下划线.但是在生产中使用服务是个坏主意.使用这样的东西代替:
import osfrom django.conf import settingsfrom django.http import httpResponse,http404def download(request,path): file_path = os.path.join(settings.MEDIA_ROOT,path) if os.path.exists(file_path): with open(file_path,'rb') as fh: response = httpResponse(fh.read(),content_type="application/vnd.ms-excel") response['Content-disposition'] = 'inline; filename=' + os.path.basename(file_path) return response raise http404总结
以上是内存溢出为你收集整理的python – Django下载文件全部内容,希望文章能够帮你解决python – Django下载文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)