Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django download a file

Tags:

python

django

I'm quite new to using Django and I am trying to develop a website where the user is able to upload a number of excel files, these files are then stored in a media folder Webproject/project/media.

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) 

The document is then displayed in a list along with any other document they have uploaded, which you can click into and it will displays basic info about them and the name of the excelfile they have uploaded. From here I want to be able to download the same excel file again using the link:

 <a  href="/project/download"> Download Document </a> 

My urls are

 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) 

but I get the error, serve() got an unexpected keyword argument 'document root'. can anyone explain how to fix this?

OR

Explain how I can get the uploaded files to to be selected and served using

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 
like image 322
Caroline Avatar asked Apr 03 '16 23:04

Caroline


People also ask

How do I upload and download in Django?

Add template folder in the Django folder and provide its path in django_folder > settings.py. Add file named as urls.py in the app folder and provide its path in django_project > urls.py. Add the function in app_folder > views.py. Provide the URL path in app> urls.py to the functions created in views.py.

How can I download Excel file in Django?

Download data as Excel file in Django: It is always recommended to user virtual environment. Once virtual environment is activated, Run this command to add xlwt package. Inside your view, import xlwt package. Use below code in your view in views.py file to create and download excel file.

How do I zip a file in Django?

The solution is as follows. Use Python module zipfile to create zip archive, but as the file specify StringIO object (ZipFile constructor requires file-like object). Add files you want to compress.


1 Answers

You missed underscore in argument document_root. But it's bad idea to use serve in production. Use something like this instead:

import os from django.conf import settings from django.http import HttpResponse, Http404  def 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 
like image 68
Sergey Gornostaev Avatar answered Sep 19 '22 16:09

Sergey Gornostaev