StorageDownloadView¶
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.
Simple example¶
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'),
)
Base options¶
StorageDownloadView inherits from DownloadMixin, which has various options such as basename or attachment.
Computing path dynamically¶
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)
API reference¶
- class django_downloadview.views.storage.StorageDownloadView(**kwargs)¶
Bases: django_downloadview.views.path.PathDownloadView
Serve a file using storage and filename.
- storage = <django.core.files.storage.DefaultStorage object at 0x31e08d0>¶
Storage the file to serve belongs to.
- path = None¶
Path to the file to serve relative to storage.
- get_path()¶
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.
- get_file()¶
Return StorageFile instance.