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

storage = FileSystemStorage()

#: Serve file using ``path`` argument.
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.urls import re_path

from demoproject.storage import views

app_name = "storage"
urlpatterns = [
    re_path(
        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

storage = FileSystemStorage()

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