Django Packages that help transfer files between projects and users
- Mar 20, 2022
Serve files with Django.
Let's serve a file stored in a file field of some model:
from django.conf.urls import url, url_patterns from django_downloadview import ObjectDownloadView from demoproject.download.models import Document # A model with a FileField # ObjectDownloadView inherits from django.views.generic.BaseDetailView. download = ObjectDownloadView.as_view(model=Document, file_field='file') url_patterns = ('', url('^download/(?P<slug>[A-Za-z0-9_-]+)/$', download, name='download'), )
xsendfile etc wrapper
from sendfile import sendfile # send myfile.pdf to user return sendfile(request, '/home/john/myfile.pdf') # send myfile.pdf as an attachment (with name myfile.pdf) return sendfile(request, '/home/john/myfile.pdf', attachment=True) # send myfile.pdf as an attachment with a different name return sendfile(request, '/home/john/myfile.pdf', attachment=True, attachment_filename='full-name.pdf')
File and Image Management Application for django