202👍
For the “best of both worlds” you could combine S.Lott’s solution with the xsendfile module: django generates the path to the file (or the file itself), but the actual file serving is handled by Apache/Lighttpd. Once you’ve set up mod_xsendfile, integrating with your view takes a few lines of code:
from django.utils.encoding import smart_str
response = HttpResponse(mimetype='application/force-download') # mimetype is replaced by content_type for django 1.7
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(path_to_file)
# It's usually a good idea to set the 'Content-Length' header too.
# You can also set any other required headers: Cache-Control, etc.
return response
Of course, this will only work if you have control over your server, or your hosting company has mod_xsendfile already set up.
EDIT:
mimetype is replaced by content_type for django 1.7
response = HttpResponse(content_type='application/force-download')
EDIT:
For nginx
check this, it uses X-Accel-Redirect
instead of apache
X-Sendfile header.
89👍
A “download” is simply an HTTP header change.
See http://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment for how to respond with a download.
You only need one URL definition for "/download"
.
The request’s GET
or POST
dictionary will have the "f=somefile.txt"
information.
Your view function will simply merge the base path with the “f
” value, open the file, create and return a response object. It should be less than 12 lines of code.
- [Django]-Django Rest Framework pagination extremely slow count
- [Django]-Django: Multiple forms possible when using FormView?
- [Django]-Django values_list vs values
37👍
For a very simple but not efficient or scalable solution, you can just use the built in django serve
view. This is excellent for quick prototypes or one-off work, but as has been mentioned throughout this question, you should use something like apache or nginx in production.
from django.views.static import serve
filepath = '/some/path/to/local/file.txt'
return serve(request, os.path.basename(filepath), os.path.dirname(filepath))
- [Django]-Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found
- [Django]-Substring in a django template?
- [Django]-Django – SQL bulk get_or_create possible?
27👍
S.Lott has the “good”/simple solution, and elo80ka has the “best”/efficient solution. Here is a “better”/middle solution – no server setup, but more efficient for large files than the naive fix:
http://djangosnippets.org/snippets/365/
Basically, Django still handles serving the file but does not load the whole thing into memory at once. This allows your server to (slowly) serve a big file without ramping up the memory usage.
Again, S.Lott’s X-SendFile is still better for larger files. But if you can’t or don’t want to bother with that, then this middle solution will gain you better efficiency without the hassle.
- [Django]-Celery. Decrease number of processes
- [Django]-Django gives Bad Request (400) when DEBUG = False
- [Django]-Cron and virtualenv
22👍
Just mentioning the FileResponse object available in Django 1.10
Edit: Just ran into my own answer while searching for an easy way to stream files via Django, so here is a more complete example (to future me). It assumes that the FileField name is imported_file
views.py
from django.views.generic.detail import DetailView
from django.http import FileResponse
class BaseFileDownloadView(DetailView):
def get(self, request, *args, **kwargs):
filename=self.kwargs.get('filename', None)
if filename is None:
raise ValueError("Found empty filename")
some_file = self.model.objects.get(imported_file=filename)
response = FileResponse(some_file.imported_file, content_type="text/csv")
# https://docs.djangoproject.com/en/1.11/howto/outputting-csv/#streaming-large-csv-files
response['Content-Disposition'] = 'attachment; filename="%s"'%filename
return response
class SomeFileDownloadView(BaseFileDownloadView):
model = SomeModel
urls.py
...
url(r'^somefile/(?P<filename>[-\w_\\-\\.]+)$', views.SomeFileDownloadView.as_view(), name='somefile-download'),
...
- [Django]-Malformed Packet: Django admin nested form can't submit, connection was reset
- [Django]-How to test Django's UpdateView?
- [Django]-Django gives Bad Request (400) when DEBUG = False
18👍
Tried @Rocketmonkeys solution but downloaded files were being stored as *.bin and given random names. That’s not fine of course. Adding another line from @elo80ka solved the problem.
Here is the code I’m using now:
from wsgiref.util import FileWrapper
from django.http import HttpResponse
filename = "/home/stackoverflow-addict/private-folder(not-p**n)/image.jpg"
wrapper = FileWrapper(file(filename))
response = HttpResponse(wrapper, content_type='text/plain')
response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(filename)
response['Content-Length'] = os.path.getsize(filename)
return response
You can now store files in a private directory (not inside /media nor /public_html) and expose them via django to certain users or under certain circumstances.
Hope it helps.
Thanks to @elo80ka, @S.Lott and @Rocketmonkeys for the answers, got the perfect solution combining all of them =)
- [Django]-How can I get the full/absolute URL (with domain) in Django?
- [Django]-How can I get MINIO access and secret key?
- [Django]-Django rest framework change primary key to use a unqiue field
13👍
It was mentioned above that the mod_xsendfile method does not allow for non-ASCII characters in filenames.
For this reason, I have a patch available for mod_xsendfile that will allow any file to be sent, as long as the name is url encoded, and the additional header:
X-SendFile-Encoding: url
Is sent as well.
- [Django]-Django-Bower + Foundation 5 + SASS, How to configure?
- [Django]-Django: how save bytes object to models.FileField?
- [Django]-Saving ModelForm error(User_Message could not be created because the data didn't validate)
7👍
Try: https://pypi.python.org/pypi/django-sendfile/
“Abstraction to offload file uploads to web-server (e.g. Apache with mod_xsendfile) once Django has checked permissions etc.”
- [Django]-How to serve media files on Django production environment?
- [Django]-Missing Table When Running Django Unittest with Sqlite3
- [Django]-What does on_delete do on Django models?
6👍
You should use sendfile apis given by popular servers like apache
or nginx
in production. For many years I was using the sendfile api of these servers for protecting files. Then created a simple middleware based django app for this purpose suitable for both development & production purposes. You can access the source code here.
UPDATE: in new version python
provider uses django FileResponse
if available and also adds support for many server implementations from lighthttp, caddy to hiawatha
Usage
pip install django-fileprovider
- add
fileprovider
app toINSTALLED_APPS
settings, - add
fileprovider.middleware.FileProviderMiddleware
toMIDDLEWARE_CLASSES
settings - set
FILEPROVIDER_NAME
settings tonginx
orapache
in production, by default it ispython
for development purpose.
in your class-based or function views, set the response header X-File
value to the absolute path of the file. For example:
def hello(request):
# code to check or protect the file from unauthorized access
response = HttpResponse()
response['X-File'] = '/absolute/path/to/file'
return response
django-fileprovider
implemented in a way that your code will need only minimum modification.
Nginx configuration
To protect file from direct access you can set the configuration as
location /files/ {
internal;
root /home/sideffect0/secret_files/;
}
Here nginx
sets a location url /files/
only access internaly, if you are using above configuration you can set X-File
as:
response['X-File'] = '/files/filename.extension'
By doing this with nginx configuration, the file will be protected & also you can control the file from django views
- [Django]-How can I get MINIO access and secret key?
- [Django]-How can I avoid "Using selector: EpollSelector" log message in Django?
- [Django]-Images from ImageField in Django don't load in template
3👍
def qrcodesave(request):
import urllib2;
url ="http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=s&chld=H|0";
opener = urllib2.urlopen(url);
content_type = "application/octet-stream"
response = HttpResponse(opener.read(), content_type=content_type)
response["Content-Disposition"]= "attachment; filename=aktel.png"
return response
- [Django]-Django rest framework, use different serializers in the same ModelViewSet
- [Django]-How do I POST with jQuery/Ajax in Django?
- [Django]-Allowing only super user login
2👍
Django recommend that you use another server to serve static media (another server running on the same machine is fine.) They recommend the use of such servers as lighttp.
This is very simple to set up. However. if ‘somefile.txt’ is generated on request (content is dynamic) then you may want django to serve it.
- [Django]-Django Rest Framework model serializer with out unique together validation
- [Django]-Django-allauth: Linking multiple social accounts to a single user
- [Django]-What is the Simplest Possible Payment Gateway to Implement? (using Django)
0👍
Another project to have a look at: http://readthedocs.org/docs/django-private-files/en/latest/usage.html
Looks promissing, haven’t tested it myself yet tho.
Basically the project abstracts the mod_xsendfile configuration and allows you to do things like:
from django.db import models
from django.contrib.auth.models import User
from private_files import PrivateFileField
def is_owner(request, instance):
return (not request.user.is_anonymous()) and request.user.is_authenticated and
instance.owner.pk = request.user.pk
class FileSubmission(models.Model):
description = models.CharField("description", max_length = 200)
owner = models.ForeignKey(User)
uploaded_file = PrivateFileField("file", upload_to = 'uploads', condition = is_owner)
- [Django]-How to delete project in django
- [Django]-How can I see the raw SQL queries Django is running?
- [Django]-Handling race condition in model.save()
0👍
I have faced the same problem more then once and so implemented using xsendfile module and auth view decorators the django-filelibrary. Feel free to use it as inspiration for your own solution.
- [Django]-Django's Double Underscore
- [Django]-Separating form input and model validation in Django?
- [Django]-Django REST framework post array of objects
0👍
Providing protected access to static html folder using https://github.com/johnsensible/django-sendfile: https://gist.github.com/iutinvg/9907731
- [Django]-Django 2.0 – Not a valid view function or pattern name (Customizing Auth views)
- [Django]-Django: Example of generic relations using the contenttypes framework?
- [Django]-Django Admin app or roll my own?
0👍
I did a project on this. You can look at my github repo:
https://github.com/nishant-boro/django-rest-framework-download-expert
This module provides a simple way to serve files for download in django rest framework using Apache module Xsendfile. It also has an additional feature of serving downloads only to users belonging to a particular group
- [Django]-Django – limiting query results
- [Django]-Form with CheckboxSelectMultiple doesn't validate
- [Django]-Django model blank=False does not work?