| 1 |
DJ-Static |
| 2 |
========= |
| 3 |
|
| 4 |
This is a simple Django middleware utility that allows you to properly |
| 5 |
serve static assets from production with a WSGI server like Gunicorn. |
| 6 |
|
| 7 |
Django `doesn't recommend <https://docs.djangoproject.com/en/1.5/howto/static-files/#admonition-serving-the-files>`_ |
| 8 |
the production use of its static file server for a number of reasons. |
| 9 |
There exists, however, a lovely WSGI application aptly named `Static <https://pypi.python.org/pypi/static>`_. |
| 10 |
|
| 11 |
.. image:: http://farm8.staticflickr.com/7387/8907351990_58677d7c35_z.jpg |
| 12 |
|
| 13 |
"Finally, a super-simple way of serving assets in Django that’ll actually perform well" — `@jacobian <https://twitter.com/jacobian/status/356754168075128833>`_ |
| 14 |
|
| 15 |
It is suitable for the production use of static file serving, unlike Django. |
| 16 |
Enjoy! |
| 17 |
|
| 18 |
Shouldn't I use a CDN? |
| 19 |
---------------------- |
| 20 |
|
| 21 |
If you have to ask that question, there's actually quite a good chance you don't. |
| 22 |
Static responses aren't very different than dynamic ones. |
| 23 |
|
| 24 |
If you're running a top-tier application, optimizing for delivery and reducing |
| 25 |
frontend load, you will want to explore using a CDN with |
| 26 |
`Django-Storages <http://django-storages.readthedocs.org/en/latest/>`_. |
| 27 |
|
| 28 |
|
| 29 |
Usage |
| 30 |
----- |
| 31 |
|
| 32 |
:: |
| 33 |
|
| 34 |
$ pip install dj-static |
| 35 |
|
| 36 |
Configure your static assets in ``settings.py``:: |
| 37 |
|
| 38 |
STATIC_ROOT = 'staticfiles' |
| 39 |
STATIC_URL = '/static/' |
| 40 |
|
| 41 |
Then, update your ``wsgi.py`` file to use dj-static:: |
| 42 |
|
| 43 |
from django.core.wsgi import get_wsgi_application |
| 44 |
from dj_static import Cling |
| 45 |
|
| 46 |
application = Cling(get_wsgi_application()) |
| 47 |
|
| 48 |
File uploads (optional) |
| 49 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 50 |
|
| 51 |
In case you also want to serve media files that were uploaded to ``MEDIA_ROOT``:: |
| 52 |
|
| 53 |
MEDIA_ROOT = 'media' |
| 54 |
MEDIA_URL = '/media/' |
| 55 |
|
| 56 |
Then again, update your ``wsgi.py`` file:: |
| 57 |
|
| 58 |
from django.core.wsgi import get_wsgi_application |
| 59 |
from dj_static import Cling, MediaCling |
| 60 |
|
| 61 |
application = Cling(MediaCling(get_wsgi_application())) |