| 1 |
################## |
| 2 |
Overview, concepts |
| 3 |
################## |
| 4 |
|
| 5 |
Given: |
| 6 |
|
| 7 |
* you manage files with Django (permissions, filters, generation, ...) |
| 8 |
|
| 9 |
* files are stored somewhere or generated somehow (local filesystem, remote |
| 10 |
storage, memory...) |
| 11 |
|
| 12 |
As a developer, you want to serve files quick and efficiently. |
| 13 |
|
| 14 |
Here is an overview of `django-downloadview`'s answer... |
| 15 |
|
| 16 |
|
| 17 |
************************************ |
| 18 |
Generic views cover commons patterns |
| 19 |
************************************ |
| 20 |
|
| 21 |
Choose the generic view depending on the file you want to serve: |
| 22 |
|
| 23 |
* :doc:`/views/object`: file field in a model; |
| 24 |
* :doc:`/views/storage`: file in a storage; |
| 25 |
* :doc:`/views/path`: absolute filename on local filesystem; |
| 26 |
* :doc:`/views/http`: file at URL (the resource is proxied); |
| 27 |
* :doc:`/views/virtual`: bytes, text, file-like objects, generated files... |
| 28 |
|
| 29 |
|
| 30 |
************************************************* |
| 31 |
Generic views and mixins allow easy customization |
| 32 |
************************************************* |
| 33 |
|
| 34 |
If your use case is a bit specific, you can easily extend the views above or |
| 35 |
:doc:`create your own based on mixins </views/custom>`. |
| 36 |
|
| 37 |
|
| 38 |
***************************** |
| 39 |
Views return DownloadResponse |
| 40 |
***************************** |
| 41 |
|
| 42 |
Views return :py:class:`~django_downloadview.response.DownloadResponse`. It is |
| 43 |
a special :py:class:`django.http.StreamingHttpResponse` where content is |
| 44 |
encapsulated in a file wrapper. |
| 45 |
|
| 46 |
Learn more in :doc:`responses`. |
| 47 |
|
| 48 |
|
| 49 |
*********************************** |
| 50 |
DownloadResponse carry file wrapper |
| 51 |
*********************************** |
| 52 |
|
| 53 |
Views instanciate a :doc:`file wrapper </files>` and use it to initialize |
| 54 |
responses. |
| 55 |
|
| 56 |
**File wrappers describe files**: they carry files properties such as name, |
| 57 |
size, encoding... |
| 58 |
|
| 59 |
**File wrappers implement loading and iterating over file content**. Whenever |
| 60 |
possible, file wrappers do not embed file data, in order to save memory. |
| 61 |
|
| 62 |
Learn more about available file wrappers in :doc:`files`. |
| 63 |
|
| 64 |
|
| 65 |
***************************************************************** |
| 66 |
Middlewares convert DownloadResponse into ProxiedDownloadResponse |
| 67 |
***************************************************************** |
| 68 |
|
| 69 |
Before WSGI application use file wrapper and actually use file contents, |
| 70 |
middlewares or decorators) are given the opportunity to capture |
| 71 |
:class:`~django_downloadview.response.DownloadResponse` instances. |
| 72 |
|
| 73 |
Let's take this opportunity to optimize file loading and streaming! |
| 74 |
|
| 75 |
A good optimization it to delegate streaming to a reverse proxy, such as |
| 76 |
`nginx`_ via `X-Accel`_ internal redirects. This way, Django doesn't load file |
| 77 |
content in memory. |
| 78 |
|
| 79 |
`django_downloadview` provides middlewares that convert |
| 80 |
:class:`~django_downloadview.response.DownloadResponse` into |
| 81 |
:class:`~django_downloadview.response.ProxiedDownloadResponse`. |
| 82 |
|
| 83 |
Learn more in :doc:`optimizations/index`. |
| 84 |
|
| 85 |
|
| 86 |
*************** |
| 87 |
Testing matters |
| 88 |
*************** |
| 89 |
|
| 90 |
`django-downloadview` also helps you :doc:`test the views you customized |
| 91 |
<testing>`. |
| 92 |
|
| 93 |
You may also :doc:`write healthchecks </healthchecks>` to make sure everything |
| 94 |
goes fine in live environments. |
| 95 |
|
| 96 |
|
| 97 |
************ |
| 98 |
What's next? |
| 99 |
************ |
| 100 |
|
| 101 |
Let's :doc:`install django-downloadview <install>`. |
| 102 |
|
| 103 |
|
| 104 |
.. rubric:: Notes & references |
| 105 |
|
| 106 |
.. target-notes:: |
| 107 |
|
| 108 |
.. _`nginx`: http://nginx.org |
| 109 |
.. _`X-Accel`: http://wiki.nginx.org/X-accel |