StorageDownloadView serves files given a storage and a path.
Use this view when you manage files in a storage (which is a good practice), unrelated to a model.
Given a storage:
from django.core.files.storage import FileSystemStorage
storage = FileSystemStorage()
Setup a view to stream files in storage:
from django_downloadview import StorageDownloadView
static_path = StorageDownloadView.as_view(storage=storage)
The view accepts a path argument you can setup either in as_view or via URLconfs:
from django.conf.urls import patterns, url
from demoproject.storage import views
urlpatterns = patterns(
'',
url(r'^static-path/(?P<path>[a-zA-Z0-9_-]+\.[a-zA-Z0-9]{1,4})$',
views.static_path,
name='static_path'),
)
Override the StorageDownloadView.get_path() method to adapt path resolution to your needs.
As an example, here is the same view as above, but the path is converted to uppercase:
from django_downloadview import StorageDownloadView
class DynamicStorageDownloadView(StorageDownloadView):
"""Serve file of storage by path.upper()."""
def get_path(self):
"""Return uppercase path."""
return super(DynamicStorageDownloadView, self).get_path().upper()
dynamic_path = DynamicStorageDownloadView.as_view(storage=storage)
Bases: django_downloadview.views.path.PathDownloadView
Serve a file using storage and filename.
Storage the file to serve belongs to.
Path to the file to serve relative to storage.
Return path of the file to serve, relative to storage.
Default implementation simply returns view’s path attribute.
Override this method if you want custom implementation.
Return StorageFile instance.