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()
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",
views.dynamic_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()
"""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>¶ 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.
-