Make your own view

DownloadMixin

The django_downloadview.views.DownloadMixin class is not a view. It is a base class which you can inherit of to create custom download views.

DownloadMixin is a base of BaseDownloadView, which itself is a base of all other django_downloadview’s builtin views.

class django_downloadview.views.base.DownloadMixin

Bases: object

Placeholders and base implementation to create file download views.

Note

This class does not inherit from django.views.generic.base.View.

The get_file() method is a placeholder subclasses must implement. Base implementation raises NotImplementedError.

Other methods provide a base implementation that use the file wrapper returned by get_file().

response_class

Response class, to be used in render_to_response().

alias of DownloadResponse

attachment = True

Whether to return the response as attachment or not.

basename = None

Client-side filename, if only file is returned as attachment.

get_file()

Return a file wrapper instance.

Raises FileNotFound if file does not exist.

get_basename()
was_modified_since(file_instance, since)

Return True if file_instance was modified after since.

Uses file wrapper’s was_modified_since if available, with value of since as positional argument.

Else, fallbacks to default implementation, which uses django.views.static.was_modified_since().

Django’s was_modified_since function needs a datetime and a size. It is passed modified_time and size attributes from file wrapper. If file wrapper does not support these attributes (AttributeError or NotImplementedError is raised), then the file is considered as modified and True is returned.

not_modified_response(*response_args, **response_kwargs)

Return django.http.HttpResponseNotModified instance.

download_response(*response_args, **response_kwargs)

Return DownloadResponse.

file_not_found_response()

Raise Http404.

render_to_response(*response_args, **response_kwargs)

Return “download” response (if everything is ok).

Return file_not_found_response() if file does not exist.

Respects the “HTTP_IF_MODIFIED_SINCE” header if any. In that case, uses was_modified_since() and not_modified_response().

Else, uses download_response() to return a download response.

BaseDownloadView

The django_downloadview.views.BaseDownloadView class is a base class to create download views. It inherits DownloadMixin and django.views.generic.base.View.

The only thing it does is to implement get: it triggers DownloadMixin's render_to_response.

class django_downloadview.views.base.BaseDownloadView(**kwargs)

Bases: django_downloadview.views.base.DownloadMixin, django.views.generic.base.View

A base DownloadMixin that implements get().

get(request, *args, **kwargs)

Handle GET requests: stream a file.

Handling http not modified responses

Sometimes, you know the latest date and time the content was generated at, and you know a new request would generate exactly the same content. In such a case, you should implement was_modified_since() in your view.

Note

Default was_modified_since() implementation trusts file wrapper’s was_modified_since if any. Else (if calling was_modified_since() raises NotImplementedError or AttributeError) it returns True, i.e. it assumes the file was modified.

As an example, the download views above always generate “Hello world!”... so, if the client already downloaded it, we can safely return some HTTP “304 Not Modified” response:

from django.core.files.base import ContentFile
from django_downloadview import VirtualDownloadView

class TextDownloadView(VirtualDownloadView):
    def get_file(self):
        """Return :class:`django.core.files.base.ContentFile` object."""
        return ContentFile(u"Hello world!", name='hello-world.txt')

    def was_modified_since(self, file_instance, since):
        return False  # Never modified, always u"Hello world!".
Read the Docs v: 1.4
Versions
latest
1.4
1.3
1.2
1.1
1.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.