user2170928
user2170928

Reputation: 247

Django download file

I'm traing to download a file in Django. I've found a lot of ways to do it and no one works for me. can anybody help me? This is my actually code in views.py:

if form.is_valid():
        format = form.cleaned_data['format']
        x.saveCalculatedRoute( tempRoute+request.user.username, format )

        path_to_file = '/var/www/tottrack/media/zones/temp/marc.osm'
        response = HttpResponse(mimetype='application/force-download')
        response['Content-Disposition'] = 'attachment; filename=prova.osm' 
        response['X-Sendfile'] = smart_str(path_to_file)

        return response

It downloads an empty file, how can I fill it?? Tanks!!

Upvotes: 0

Views: 1622

Answers (1)

Matt Seymour
Matt Seymour

Reputation: 9395

I personally would put this file into the media directory and let your server do the streaming of the file rather than django unless you really need it to.

serving static files in django is really inefficient so your always better off doing this through the web server which has been specifically optimized for serving data.

From the django docs https://docs.djangoproject.com/en/1.5/howto/static-files/#configuring-static-files

Serving the files

In addition to these configuration steps, you’ll also need to actually serve the static files.

During development, this will be done automatically if you use runserver and DEBUG is set to True (see django.contrib.staticfiles.views.serve()).

This method is grossly inefficient and probably insecure, so it is unsuitable for production.

See Deploying static files for proper strategies to serve static files in production environments.

Upvotes: 1

Related Questions