Responses

Views return DownloadResponse.

Middlewares (and decorators) are given the opportunity to capture responses and convert them to ProxiedDownloadResponse.

DownloadResponse

class django_downloadview.response.DownloadResponse(file_instance, attachment=True, basename=None, status=200, content_type=None, file_mimetype=None, file_encoding=None)

Bases: django.http.response.StreamingHttpResponse

File download response (Django serves file, client downloads it).

This is a specialization of django.http.StreamingHttpResponse where streaming_content is a file wrapper.

Constructor differs a bit from HttpResponse.

Here are some highlights to understand internal mechanisms and motivations:

  • Let’s start by quoting PEP 3333 (WSGI specification):

    For large files, or for specialized uses of HTTP streaming, applications will usually return an iterator (often a generator-iterator) that produces the output in a block-by-block fashion.

  • Django WSGI handler (application implementation) returns response object (see django.core.handlers.wsgi).

  • django.http.HttpResponse and subclasses are iterators.

  • In StreamingHttpResponse, the __iter__() implementation proxies to streaming_content.

  • In DownloadResponse and subclasses, streaming_content is a file wrapper. File wrapper is itself an iterator over actual file content, and it also encapsulates access to file attributes (size, name, ...).

file = None

A file wrapper instance, such as File.

basename = None

Client-side name of the file to stream. Only used if attachment is True. Affects Content-Disposition header.

attachment = None

Whether to return the file as attachment or not. Affects Content-Disposition header.

file_mimetype = None

Value for file’s mimetype. If None (the default), then the file’s mimetype will be guessed via Python’s mimetypes. See get_mime_type().

file_encoding = None

Value for file’s encoding. If None (the default), then the file’s encoding will be guessed via Python’s mimetypes. See get_encoding().

default_headers

Return dictionary of automatically-computed headers.

Uses an internal _default_headers cache. Default values are computed if only cache hasn’t been set.

Content-Disposition header is encoded according to RFC 5987. See also http://stackoverflow.com/questions/93551/.

items()

Return iterable of (header, value).

This method is called by http handlers just before WSGI’s start_response() is called... but it is not called by django.test.ClientHandler! :’(

get_basename()

Return basename.

get_content_type()

Return a suitable “Content-Type” header for self.file.

get_mime_type()

Return mime-type of the file.

get_encoding()

Return encoding of the file to serve.

get_charset()

Return the charset of the file to serve.

ProxiedDownloadResponse

class django_downloadview.response.ProxiedDownloadResponse(content='', *args, **kwargs)

Bases: django.http.response.HttpResponse

Base class for internal redirect download responses.

This base class makes it possible to identify several types of specific responses such as XAccelRedirectResponse.