Configure

Here is the list of Django settings for django-downloadview.

INSTALLED_APPS

There is no need to register this application in INSTALLED_APPS.

MIDDLEWARE_CLASSES

If you plan to setup reverse-proxy optimizations, add django_downloadview.SmartDownloadMiddleware to MIDDLEWARE_CLASSES. It is a response middleware. Move it after middlewares that compute the response content such as gzip middleware.

Example:

MIDDLEWARE = [
    "django.middleware.common.CommonMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django_downloadview.SmartDownloadMiddleware",
]

DEFAULT_FILE_STORAGE

django-downloadview offers a built-in signed file storage, which cryptographically signs requested file URLs with the Django’s built-in TimeStampSigner.

To utilize the signed storage views you can configure

DEFAULT_FILE_STORAGE='django_downloadview.storage.SignedStorage'

The signed file storage system inserts a X-Signature header to the requested file URLs, and they can then be verified with the supplied signature_required wrapper function:

from django.conf.urls import url, url_patterns

from django_downloadview import ObjectDownloadView
from django_downloadview.decorators import signature_required

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')

 urlpatterns = [
     path('download/<str:slug>/', signature_required(download),
 ]

Make sure to test the desired functionality after configuration.

DOWNLOADVIEW_URL_EXPIRATION

Number of seconds signed download URLs are valid before expiring.

Default value for this flag is None and URLs never expire.

DOWNLOADVIEW_BACKEND

This setting is used by SmartDownloadMiddleware. It is the import string of a callable (typically a class) of an optimization backend (typically a BaseDownloadMiddleware subclass).

Example:

DOWNLOADVIEW_BACKEND = "django_downloadview.nginx.XAccelRedirectMiddleware"

See Optimize streaming for a list of available backends (middlewares).

When django_downloadview.SmartDownloadMiddleware is in your MIDDLEWARE_CLASSES, this setting must be explicitely configured (no default value). Else, you can ignore this setting.

DOWNLOADVIEW_RULES

This setting is used by SmartDownloadMiddleware. It is a list of positional arguments or keyword arguments that will be used to instanciate class mentioned as DOWNLOADVIEW_BACKEND.

Each item in the list can be either a list of positional arguments, or a dictionary of keyword arguments. One item cannot contain both positional and keyword arguments.

Here is an example containing one rule using keyword arguments:

DOWNLOADVIEW_RULES = [
    {
        "source_url": "/media/nginx/",
        "destination_url": "/nginx-optimized-by-middleware/",
    },
]

See Optimize streaming for details about builtin backends (middlewares) and their options.

When django_downloadview.SmartDownloadMiddleware is in your MIDDLEWARE_CLASSES, this setting must be explicitely configured (no default value). Else, you can ignore this setting.