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:
StreamingHttpResponseFile download response (Django serves file, client downloads it).
This is a specialization of
django.http.StreamingHttpResponsewherestreaming_contentis 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.HttpResponseand subclasses are iterators.In
StreamingHttpResponse, the__iter__()implementation proxies tostreaming_content.In
DownloadResponseand subclasses,streaming_contentis a file wrapper. File wrapper is itself an iterator over actual file content, and it also encapsulates access to file attributes (size, name, …).
- file¶
A file wrapper instance, such as
File.
- basename¶
Client-side name of the file to stream. Only used if
attachmentisTrue. AffectsContent-Dispositionheader.
- attachment¶
Whether to return the file as attachment or not. Affects
Content-Dispositionheader.
- file_mimetype¶
Value for file’s mimetype. If
None(the default), then the file’s mimetype will be guessed via Python’smimetypes. Seeget_mime_type().
- file_encoding¶
Value for file’s encoding. If
None(the default), then the file’s encoding will be guessed via Python’smimetypes. Seeget_encoding().
- property default_headers¶
Return dictionary of automatically-computed headers.
Uses an internal
_default_headerscache. Default values are computed if only cache hasn’t been set.Content-Dispositionheader is encoded according to RFC 5987. See also http://stackoverflow.com/questions/93551/.
- 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=b'', *args, **kwargs)¶
Bases:
HttpResponseBase class for internal redirect download responses.
This base class makes it possible to identify several types of specific responses such as
XAccelRedirectResponse.