| 1 |
################ |
| 2 |
PathDownloadView |
| 3 |
################ |
| 4 |
|
| 5 |
.. py:module:: django_downloadview.views.path |
| 6 |
|
| 7 |
:class:`PathDownloadView` **serves file given a path on local filesystem**. |
| 8 |
|
| 9 |
Use this view whenever you just have a path, outside storage or model. |
| 10 |
|
| 11 |
.. warning:: |
| 12 |
|
| 13 |
Take care of path validation, especially if you compute paths from user |
| 14 |
input: an attacker may be able to download files from arbitrary locations. |
| 15 |
In most cases, you should consider managing files in storages, because they |
| 16 |
implement default security mechanisms. |
| 17 |
|
| 18 |
|
| 19 |
************** |
| 20 |
Simple example |
| 21 |
************** |
| 22 |
|
| 23 |
Setup a view to stream files given path: |
| 24 |
|
| 25 |
.. literalinclude:: /../demo/demoproject/path/views.py |
| 26 |
:language: python |
| 27 |
:lines: 1-13 |
| 28 |
:emphasize-lines: 13 |
| 29 |
|
| 30 |
|
| 31 |
************ |
| 32 |
Base options |
| 33 |
************ |
| 34 |
|
| 35 |
:class:`PathDownloadView` inherits from |
| 36 |
:class:`~django_downloadview.views.base.DownloadMixin`, which has various |
| 37 |
options such as :attr:`~django_downloadview.views.base.DownloadMixin.basename` |
| 38 |
or :attr:`~django_downloadview.views.base.DownloadMixin.attachment`. |
| 39 |
|
| 40 |
|
| 41 |
************************** |
| 42 |
Computing path dynamically |
| 43 |
************************** |
| 44 |
|
| 45 |
Override the :meth:`PathDownloadView.get_path` method to adapt path |
| 46 |
resolution to your needs: |
| 47 |
|
| 48 |
.. literalinclude:: /../demo/demoproject/path/views.py |
| 49 |
:language: python |
| 50 |
:lines: 1-9, 15- |
| 51 |
|
| 52 |
The view accepts a ``path`` argument you can setup either in ``as_view`` or |
| 53 |
via URLconfs: |
| 54 |
|
| 55 |
.. literalinclude:: /../demo/demoproject/path/urls.py |
| 56 |
:language: python |
| 57 |
:lines: 1-13 |
| 58 |
|
| 59 |
|
| 60 |
************* |
| 61 |
API reference |
| 62 |
************* |
| 63 |
|
| 64 |
.. autoclass:: PathDownloadView |
| 65 |
:members: |
| 66 |
:undoc-members: |
| 67 |
:show-inheritance: |
| 68 |
:member-order: bysource |