2010-10-20 09:33:24 +08:00
|
|
|
=====================
|
|
|
|
Managing static files
|
|
|
|
=====================
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
.. versionadded:: 1.3
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
Django developers mostly concern themselves with the dynamic parts of web
|
2011-02-01 06:06:53 +08:00
|
|
|
applications -- the views and templates that render anew for each request. But
|
2011-01-31 01:23:25 +08:00
|
|
|
web applications have other parts: the static files (images, CSS,
|
2010-10-20 09:33:24 +08:00
|
|
|
Javascript, etc.) that are needed to render a complete web page.
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
For small projects, this isn't a big deal, because you can just keep the
|
|
|
|
static files somewhere your web server can find it. However, in bigger
|
|
|
|
projects -- especially those comprised of multiple apps -- dealing with the
|
|
|
|
multiple sets of static files provided by each application starts to get
|
|
|
|
tricky.
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
That's what ``django.contrib.staticfiles`` is for: it collects static files
|
|
|
|
from each of your applications (and any other places you specify) into a
|
|
|
|
single location that can easily be served in production.
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
.. note::
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
If you've used the `django-staticfiles`_ third-party app before, then
|
|
|
|
``django.contrib.staticfiles`` will look very familiar. That's because
|
|
|
|
they're essentially the same code: ``django.contrib.staticfiles`` started
|
|
|
|
its life as `django-staticfiles`_ and was merged into Django 1.3.
|
2010-10-26 20:32:35 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
If you're upgrading from ``django-staticfiles``, please see `Upgrading from
|
|
|
|
django-staticfiles`_, below, for a few minor changes you'll need to make.
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
.. _django-staticfiles: http://pypi.python.org/pypi/django-staticfiles/
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
Using ``django.contrib.staticfiles``
|
|
|
|
====================================
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
Basic usage
|
|
|
|
-----------
|
2005-11-27 22:49:57 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
1. Put your static files somewhere that ``staticfiles`` will find them.
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
By default, this means within ``static/`` subdirectories of apps in your
|
|
|
|
:setting:`INSTALLED_APPS`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
Your project will probably also have static assets that aren't tied to a
|
|
|
|
particular app. The :setting:`STATICFILES_DIRS` setting is a tuple of
|
|
|
|
filesystem directories to check when loading static files. It's a search
|
|
|
|
path that is by default empty. See the :setting:`STATICFILES_DIRS` docs
|
|
|
|
how to extend this list of additional paths.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
Additionally, see the documentation for the :setting:`STATICFILES_FINDERS`
|
|
|
|
setting for details on how ``staticfiles`` finds your files.
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
2. Make sure that ``django.contrib.staticfiles`` is included in your
|
|
|
|
:setting:`INSTALLED_APPS`.
|
2011-03-25 01:52:32 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
For :ref:`local development<staticfiles-development>`, if you are using
|
|
|
|
:ref:`runserver<staticfiles-runserver>` or adding
|
|
|
|
:ref:`staticfiles_urlpatterns<staticfiles-development>` to your
|
|
|
|
URLconf, you're done with the setup -- your static files will
|
|
|
|
automatically be served at the default (for
|
|
|
|
:djadmin:`newly created<startproject>` projects) :setting:`STATIC_URL`
|
|
|
|
of ``/static/``.
|
2011-03-25 01:52:32 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
3. You'll probably need to refer to these files in your templates. The
|
|
|
|
easiest method is to use the included context processor which allows
|
|
|
|
template code like:
|
2011-03-25 01:52:32 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
.. code-block:: html+django
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2012-06-06 16:32:03 +08:00
|
|
|
<img src="{{ STATIC_URL }}images/hi.jpg" alt="Hi!" />
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
See :ref:`staticfiles-in-templates` for more details, **including** an
|
2011-05-18 17:51:24 +08:00
|
|
|
alternate method using a template tag.
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
Deploying static files in a nutshell
|
|
|
|
------------------------------------
|
2008-10-06 16:29:05 +08:00
|
|
|
|
2011-02-01 06:06:53 +08:00
|
|
|
When you're ready to move out of local development and deploy your project:
|
2010-11-12 05:43:49 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
1. Set the :setting:`STATIC_URL` setting to the public URL for your static
|
|
|
|
files (in most cases, the default value of ``/static/`` is just fine).
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
2. Set the :setting:`STATIC_ROOT` setting to point to the filesystem path
|
|
|
|
you'd like your static files collected to when you use the
|
|
|
|
:djadmin:`collectstatic` management command. For example::
|
2009-04-01 07:34:03 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
STATIC_ROOT = "/home/jacob/projects/mysite.com/sitestatic"
|
2005-11-27 23:28:19 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
3. Run the :djadmin:`collectstatic` management command::
|
2005-11-27 23:28:19 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
./manage.py collectstatic
|
2005-11-27 23:28:19 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
This'll churn through your static file storage and copy them into the
|
|
|
|
directory given by :setting:`STATIC_ROOT`.
|
2005-11-27 23:28:19 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
4. Deploy those files by configuring your webserver of choice to serve the
|
|
|
|
files in :setting:`STATIC_ROOT` at :setting:`STATIC_URL`.
|
2005-11-27 23:28:19 +08:00
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
:ref:`staticfiles-production` covers some common deployment strategies
|
|
|
|
for static files.
|
|
|
|
|
|
|
|
Those are the **basics**. For more details on common configuration options,
|
|
|
|
read on; for a detailed reference of the settings, commands, and other bits
|
|
|
|
included with the framework see
|
|
|
|
:doc:`the staticfiles reference </ref/contrib/staticfiles>`.
|
2005-11-27 23:28:19 +08:00
|
|
|
|
2010-11-12 05:43:49 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
In previous versions of Django, it was common to place static assets in
|
2011-05-18 17:51:24 +08:00
|
|
|
:setting:`MEDIA_ROOT` along with user-uploaded files, and serve them both
|
|
|
|
at :setting:`MEDIA_URL`. Part of the purpose of introducing the
|
|
|
|
``staticfiles`` app is to make it easier to keep static files separate
|
|
|
|
from user-uploaded files.
|
|
|
|
|
|
|
|
For this reason, you need to make your :setting:`MEDIA_ROOT` and
|
2011-01-31 01:23:25 +08:00
|
|
|
:setting:`MEDIA_URL` different from your :setting:`STATIC_ROOT` and
|
|
|
|
:setting:`STATIC_URL`. You will need to arrange for serving of files in
|
|
|
|
:setting:`MEDIA_ROOT` yourself; ``staticfiles`` does not deal with
|
2011-02-14 09:42:26 +08:00
|
|
|
user-uploaded files at all. You can, however, use
|
2011-05-18 17:51:24 +08:00
|
|
|
:func:`django.views.static.serve` view for serving :setting:`MEDIA_ROOT`
|
2011-02-14 09:42:26 +08:00
|
|
|
in development; see :ref:`staticfiles-other-directories`.
|
2010-11-12 05:43:49 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
.. _staticfiles-in-templates:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
Referring to static files in templates
|
|
|
|
======================================
|
|
|
|
|
|
|
|
At some point, you'll probably need to link to static files in your templates.
|
|
|
|
You could, of course, simply hardcode the path to you assets in the templates:
|
|
|
|
|
|
|
|
.. code-block:: html
|
|
|
|
|
2012-06-06 16:32:03 +08:00
|
|
|
<img src="http://static.example.com/static/myimage.jpg" alt="Sample image" />
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
Of course, there are some serious problems with this: it doesn't work well in
|
|
|
|
development, and it makes it *very* hard to change where you've deployed your
|
2011-01-31 01:23:25 +08:00
|
|
|
static files. If, for example, you wanted to switch to using a content
|
|
|
|
delivery network (CDN), then you'd need to change more or less every single
|
|
|
|
template.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2010-11-17 23:36:26 +08:00
|
|
|
A far better way is to use the value of the :setting:`STATIC_URL` setting
|
2011-01-31 01:23:25 +08:00
|
|
|
directly in your templates. This means that a switch of static files servers
|
|
|
|
only requires changing that single value. Much better!
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
Django includes multiple built-in ways of using this setting in your
|
2010-10-20 09:33:24 +08:00
|
|
|
templates: a context processor and a template tag.
|
2005-11-27 23:28:19 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
With a context processor
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
The included context processor is the easy way. Simply make sure
|
2010-11-17 23:36:26 +08:00
|
|
|
``'django.core.context_processors.static'`` is in your
|
2010-10-20 09:33:24 +08:00
|
|
|
:setting:`TEMPLATE_CONTEXT_PROCESSORS`. It's there by default, and if you're
|
|
|
|
editing that setting by hand it should look something like::
|
|
|
|
|
|
|
|
TEMPLATE_CONTEXT_PROCESSORS = (
|
|
|
|
'django.core.context_processors.debug',
|
|
|
|
'django.core.context_processors.i18n',
|
2010-11-12 05:44:33 +08:00
|
|
|
'django.core.context_processors.media',
|
2010-11-17 23:36:26 +08:00
|
|
|
'django.core.context_processors.static',
|
2010-10-20 09:33:24 +08:00
|
|
|
'django.contrib.auth.context_processors.auth',
|
|
|
|
'django.contrib.messages.context_processors.messages',
|
2005-11-27 22:42:21 +08:00
|
|
|
)
|
|
|
|
|
2010-11-17 23:36:26 +08:00
|
|
|
Once that's done, you can refer to :setting:`STATIC_URL` in your templates:
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
2012-06-06 16:32:03 +08:00
|
|
|
<img src="{{ STATIC_URL }}images/hi.jpg" alt="Hi!" />
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2010-11-17 23:36:26 +08:00
|
|
|
If ``{{ STATIC_URL }}`` isn't working in your template, you're probably not
|
2010-10-20 09:33:24 +08:00
|
|
|
using :class:`~django.template.RequestContext` when rendering the template.
|
|
|
|
|
|
|
|
As a brief refresher, context processors add variables into the contexts of
|
|
|
|
every template. However, context processors require that you use
|
|
|
|
:class:`~django.template.RequestContext` when rendering templates. This happens
|
|
|
|
automatically if you're using a :doc:`generic view </ref/class-based-views>`,
|
2011-05-13 12:33:42 +08:00
|
|
|
but in views written by hand you'll need to explicitly use ``RequestContext``
|
2010-10-20 09:33:24 +08:00
|
|
|
To see how that works, and to read more details, check out
|
|
|
|
:ref:`subclassing-context-requestcontext`.
|
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
Another option is the :ttag:`get_static_prefix` template tag that is part of
|
|
|
|
Django's core.
|
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
With a template tag
|
|
|
|
-------------------
|
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
The more powerful tool is the :ttag:`static<staticfiles-static>` template
|
|
|
|
tag. It builds the URL for the given relative path by using the configured
|
|
|
|
:setting:`STATICFILES_STORAGE` storage.
|
2011-06-30 17:06:19 +08:00
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
{% load staticfiles %}
|
2012-06-06 16:32:03 +08:00
|
|
|
<img src="{% static "images/hi.jpg" %}" alt="Hi!"/>
|
2011-06-30 17:06:19 +08:00
|
|
|
|
|
|
|
It is also able to consume standard context variables, e.g. assuming a
|
|
|
|
``user_stylesheet`` variable is passed to the template:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
{% load staticfiles %}
|
2011-06-30 17:06:19 +08:00
|
|
|
<link rel="stylesheet" href="{% static user_stylesheet %}" type="text/css" media="screen" />
|
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
.. note::
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
There is also a template tag named :ttag:`static` in Django's core set
|
|
|
|
of :ref:`built in template tags<ref-templates-builtins-tags>` which has
|
|
|
|
the same argument signature but only uses `urlparse.urljoin()`_ with the
|
|
|
|
:setting:`STATIC_URL` setting and the given path. This has the
|
|
|
|
disadvantage of not being able to easily switch the storage backend
|
|
|
|
without changing the templates, so in doubt use the ``staticfiles``
|
|
|
|
:ttag:`static<staticfiles-static>`
|
|
|
|
template tag.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
.. _`urlparse.urljoin()`: http://docs.python.org/library/urlparse.html#urlparse.urljoin
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
.. _staticfiles-development:
|
|
|
|
|
|
|
|
Serving static files in development
|
|
|
|
===================================
|
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
The static files tools are mostly designed to help with getting static files
|
|
|
|
successfully deployed into production. This usually means a separate,
|
|
|
|
dedicated static file server, which is a lot of overhead to mess with when
|
|
|
|
developing locally. Thus, the ``staticfiles`` app ships with a
|
|
|
|
**quick and dirty helper view** that you can use to serve files locally in
|
|
|
|
development.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2010-11-12 05:43:49 +08:00
|
|
|
This view is automatically enabled and will serve your static files at
|
2011-02-01 06:06:53 +08:00
|
|
|
:setting:`STATIC_URL` when you use the built-in
|
|
|
|
:ref:`runserver<staticfiles-runserver>` management command.
|
2010-11-12 05:43:49 +08:00
|
|
|
|
|
|
|
To enable this view if you are using some other server for local development,
|
2011-01-31 01:23:25 +08:00
|
|
|
you'll add a couple of lines to your URLconf. The first line goes at the top
|
|
|
|
of the file, and the last line at the bottom::
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
|
|
|
|
|
|
|
|
# ... the rest of your URLconf goes here ...
|
|
|
|
|
|
|
|
urlpatterns += staticfiles_urlpatterns()
|
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
This will inspect your :setting:`STATIC_URL` setting and wire up the view
|
|
|
|
to serve static files accordingly. Don't forget to set the
|
|
|
|
:setting:`STATICFILES_DIRS` setting appropriately to let
|
|
|
|
``django.contrib.staticfiles`` know where to look for files additionally to
|
|
|
|
files in app directories.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
This will only work if :setting:`DEBUG` is ``True``.
|
|
|
|
|
|
|
|
That's because this view is **grossly inefficient** and probably
|
|
|
|
**insecure**. This is only intended for local development, and should
|
|
|
|
**never be used in production**.
|
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
Additionally, when using ``staticfiles_urlpatterns`` your
|
|
|
|
:setting:`STATIC_URL` setting can't be empty or a full URL, such as
|
|
|
|
``http://static.example.com/``.
|
2010-11-17 23:36:26 +08:00
|
|
|
|
2011-02-14 09:42:26 +08:00
|
|
|
For a few more details on how the ``staticfiles`` can be used during
|
|
|
|
development, see :ref:`staticfiles-development-view`.
|
|
|
|
|
|
|
|
.. _staticfiles-other-directories:
|
|
|
|
|
|
|
|
Serving other directories
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
.. currentmodule:: django.views.static
|
|
|
|
.. function:: serve(request, path, document_root, show_indexes=False)
|
|
|
|
|
|
|
|
There may be files other than your project's static assets that, for
|
|
|
|
convenience, you'd like to have Django serve for you in local development.
|
|
|
|
The :func:`~django.views.static.serve` view can be used to serve any directory
|
|
|
|
you give it. (Again, this view is **not** hardened for production
|
|
|
|
use, and should be used only as a development aid; you should serve these files
|
|
|
|
in production using a real front-end webserver).
|
|
|
|
|
|
|
|
The most likely example is user-uploaded content in :setting:`MEDIA_ROOT`.
|
|
|
|
``staticfiles`` is intended for static assets and has no built-in handling
|
|
|
|
for user-uploaded files, but you can have Django serve your
|
|
|
|
:setting:`MEDIA_ROOT` by appending something like this to your URLconf::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
# ... the rest of your URLconf goes here ...
|
|
|
|
|
|
|
|
if settings.DEBUG:
|
|
|
|
urlpatterns += patterns('',
|
2011-02-17 10:11:44 +08:00
|
|
|
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
|
2011-02-14 09:42:26 +08:00
|
|
|
'document_root': settings.MEDIA_ROOT,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
Note, the snippet assumes your :setting:`MEDIA_URL` has a value of
|
|
|
|
``'/media/'``. This will call the :func:`~django.views.static.serve` view,
|
|
|
|
passing in the path from the URLconf and the (required) ``document_root``
|
|
|
|
parameter.
|
|
|
|
|
|
|
|
.. currentmodule:: django.conf.urls.static
|
|
|
|
.. function:: static(prefix, view='django.views.static.serve', **kwargs)
|
|
|
|
|
|
|
|
Since it can become a bit cumbersome to define this URL pattern, Django
|
|
|
|
ships with a small URL helper function
|
2011-05-10 06:30:31 +08:00
|
|
|
:func:`~django.conf.urls.static.static` that takes as parameters the prefix
|
2011-02-14 09:42:26 +08:00
|
|
|
such as :setting:`MEDIA_URL` and a dotted path to a view, such as
|
|
|
|
``'django.views.static.serve'``. Any other function parameter will be
|
|
|
|
transparently passed to the view.
|
|
|
|
|
|
|
|
An example for serving :setting:`MEDIA_URL` (``'/media/'``) during
|
|
|
|
development::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.conf.urls.static import static
|
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
# ... the rest of your URLconf goes here ...
|
|
|
|
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-03-16 04:13:21 +08:00
|
|
|
.. note::
|
|
|
|
|
2011-05-18 17:51:24 +08:00
|
|
|
This helper function will only be operational in debug mode and if
|
2011-03-16 04:13:21 +08:00
|
|
|
the given prefix is local (e.g. ``/static/``) and not a URL (e.g.
|
|
|
|
``http://static.example.com/``).
|
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
.. _staticfiles-production:
|
|
|
|
|
|
|
|
Serving static files in production
|
|
|
|
==================================
|
|
|
|
|
2010-10-21 01:57:37 +08:00
|
|
|
The basic outline of putting static files into production is simple: run the
|
2011-01-31 01:23:25 +08:00
|
|
|
:djadmin:`collectstatic` command when static files change, then arrange for
|
|
|
|
the collected static files directory (:setting:`STATIC_ROOT`) to be moved to
|
|
|
|
the static file server and served.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
Of course, as with all deployment tasks, the devil's in the details. Every
|
|
|
|
production setup will be a bit different, so you'll need to adapt the basic
|
|
|
|
outline to fit your needs. Below are a few common patterns that might help.
|
|
|
|
|
|
|
|
Serving the app and your static files from the same server
|
|
|
|
----------------------------------------------------------
|
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
If you want to serve your static files from the same server that's already
|
|
|
|
serving your site, the basic outline gets modified to look something like:
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Push your code up to the deployment server.
|
|
|
|
* On the server, run :djadmin:`collectstatic` to copy all the static files
|
|
|
|
into :setting:`STATIC_ROOT`.
|
|
|
|
* Point your web server at :setting:`STATIC_ROOT`. For example, here's
|
|
|
|
:ref:`how to do this under Apache and mod_wsgi <serving-files>`.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
You'll probably want to automate this process, especially if you've got
|
|
|
|
multiple web servers. There's any number of ways to do this automation, but
|
|
|
|
one option that many Django developers enjoy is `Fabric`__.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
__ http://fabfile.org/
|
|
|
|
|
|
|
|
Below, and in the following sections, we'll show off a few example fabfiles
|
2011-01-31 01:23:25 +08:00
|
|
|
(i.e. Fabric scripts) that automate these file deployment options. The syntax
|
2010-10-26 20:32:35 +08:00
|
|
|
of a fabfile is fairly straightforward but won't be covered here; consult
|
2010-10-23 22:32:20 +08:00
|
|
|
`Fabric's documentation`__, for a complete explanation of the syntax..
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
__ http://docs.fabfile.org/
|
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
So, a fabfile to deploy static files to a couple of web servers might look
|
|
|
|
something like::
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
from fabric.api import *
|
|
|
|
|
|
|
|
# Hosts to deploy onto
|
|
|
|
env.hosts = ['www1.example.com', 'www2.example.com']
|
|
|
|
|
|
|
|
# Where your project code lives on the server
|
|
|
|
env.project_root = '/home/www/myproject'
|
|
|
|
|
|
|
|
def deploy_static():
|
|
|
|
with cd(env.project_root):
|
2011-01-17 05:49:01 +08:00
|
|
|
run('./manage.py collectstatic -v0 --noinput')
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
Serving static files from a dedicated server
|
|
|
|
--------------------------------------------
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
Most larger Django apps use a separate Web server -- i.e., one that's not also
|
2011-01-31 01:23:25 +08:00
|
|
|
running Django -- for serving static files. This server often runs a different
|
|
|
|
type of web server -- faster but less full-featured. Some good choices are:
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* lighttpd_
|
|
|
|
* Nginx_
|
|
|
|
* TUX_
|
|
|
|
* Cherokee_
|
|
|
|
* A stripped-down version of Apache_
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
.. _lighttpd: http://www.lighttpd.net/
|
|
|
|
.. _Nginx: http://wiki.nginx.org/Main
|
|
|
|
.. _TUX: http://en.wikipedia.org/wiki/TUX_web_server
|
|
|
|
.. _Apache: http://httpd.apache.org/
|
|
|
|
.. _Cherokee: http://www.cherokee-project.com/
|
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
Configuring these servers is out of scope of this document; check each
|
|
|
|
server's respective documentation for instructions.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
Since your static file server won't be running Django, you'll need to modify
|
|
|
|
the deployment strategy to look something like:
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* When your static files change, run :djadmin:`collectstatic` locally.
|
|
|
|
* Push your local :setting:`STATIC_ROOT` up to the static file server
|
|
|
|
into the directory that's being served. ``rsync`` is a good
|
|
|
|
choice for this step since it only needs to transfer the
|
|
|
|
bits of static files that have changed.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
Here's how this might look in a fabfile::
|
|
|
|
|
|
|
|
from fabric.api import *
|
|
|
|
from fabric.contrib import project
|
|
|
|
|
|
|
|
# Where the static files get collected locally
|
|
|
|
env.local_static_root = '/tmp/static'
|
|
|
|
|
|
|
|
# Where the static files should go remotely
|
2011-01-31 01:23:25 +08:00
|
|
|
env.remote_static_root = '/home/www/static.example.com'
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
@roles('static')
|
2010-10-20 09:33:24 +08:00
|
|
|
def deploy_static():
|
|
|
|
local('./manage.py collectstatic')
|
2012-04-30 19:52:37 +08:00
|
|
|
project.rsync_project(
|
2010-10-20 09:33:24 +08:00
|
|
|
remote_dir = env.remote_static_root,
|
|
|
|
local_dir = env.local_static_root,
|
|
|
|
delete = True
|
2005-11-27 22:42:21 +08:00
|
|
|
)
|
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
.. _staticfiles-from-cdn:
|
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
Serving static files from a cloud service or CDN
|
2010-10-20 09:33:24 +08:00
|
|
|
------------------------------------------------
|
|
|
|
|
2011-01-31 01:23:25 +08:00
|
|
|
Another common tactic is to serve static files from a cloud storage provider
|
|
|
|
like Amazon's S3__ and/or a CDN (content delivery network). This lets you
|
|
|
|
ignore the problems of serving static files, and can often make for
|
|
|
|
faster-loading webpages (especially when using a CDN).
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
When using these services, the basic workflow would look a bit like the above,
|
2011-01-31 01:23:25 +08:00
|
|
|
except that instead of using ``rsync`` to transfer your static files to the
|
|
|
|
server you'd need to transfer the static files to the storage provider or CDN.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
There's any number of ways you might do this, but if the provider has an API a
|
|
|
|
:doc:`custom file storage backend </howto/custom-file-storage>` will make the
|
|
|
|
process incredibly simple. If you've written or are using a 3rd party custom
|
|
|
|
storage backend, you can tell :djadmin:`collectstatic` to use it by setting
|
|
|
|
:setting:`STATICFILES_STORAGE` to the storage engine.
|
|
|
|
|
|
|
|
For example, if you've written an S3 storage backend in
|
|
|
|
``myproject.storage.S3Storage`` you could use it with::
|
|
|
|
|
2010-10-23 22:32:20 +08:00
|
|
|
STATICFILES_STORAGE = 'myproject.storage.S3Storage'
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
Once that's done, all you have to do is run :djadmin:`collectstatic` and your
|
2011-01-31 01:23:25 +08:00
|
|
|
static files would be pushed through your storage package up to S3. If you
|
2011-05-13 12:33:42 +08:00
|
|
|
later needed to switch to a different storage provider, it could be as simple
|
2011-01-31 01:23:25 +08:00
|
|
|
as changing your :setting:`STATICFILES_STORAGE` setting.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
For details on how you'd write one of these backends,
|
|
|
|
:doc:`/howto/custom-file-storage`.
|
|
|
|
|
|
|
|
.. seealso::
|
2005-11-27 22:42:21 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
The `django-storages`__ project is a 3rd party app that provides many
|
2010-10-23 22:32:20 +08:00
|
|
|
storage backends for many common file storage APIs (including `S3`__).
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
__ http://s3.amazonaws.com/
|
2012-03-14 15:41:02 +08:00
|
|
|
__ http://code.larlet.fr/django-storages/
|
|
|
|
__ http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
Upgrading from ``django-staticfiles``
|
|
|
|
=====================================
|
|
|
|
|
|
|
|
``django.contrib.staticfiles`` began its life as `django-staticfiles`_. If
|
2011-02-14 09:42:26 +08:00
|
|
|
you're upgrading from `django-staticfiles`_ older than 1.0 (e.g. 0.3.4) to
|
2011-01-31 01:23:25 +08:00
|
|
|
``django.contrib.staticfiles``, you'll need to make a few changes:
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Application files should now live in a ``static`` directory in each app
|
|
|
|
(`django-staticfiles`_ used the name ``media``, which was slightly
|
|
|
|
confusing).
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The management commands ``build_static`` and ``resolve_static`` are now
|
|
|
|
called :djadmin:`collectstatic` and :djadmin:`findstatic`.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The settings ``STATICFILES_PREPEND_LABEL_APPS``,
|
|
|
|
``STATICFILES_MEDIA_DIRNAMES`` and ``STATICFILES_EXCLUDED_APPS`` were
|
|
|
|
removed.
|
2010-10-26 20:32:35 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The setting ``STATICFILES_RESOLVERS`` was removed, and replaced by the
|
|
|
|
new :setting:`STATICFILES_FINDERS`.
|
2010-10-26 20:32:35 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The default for :setting:`STATICFILES_STORAGE` was renamed from
|
|
|
|
``staticfiles.storage.StaticFileStorage`` to
|
|
|
|
``staticfiles.storage.StaticFilesStorage``
|
2010-10-26 20:32:35 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* If using :ref:`runserver<staticfiles-runserver>` for local development
|
|
|
|
(and the :setting:`DEBUG` setting is ``True``), you no longer need to add
|
|
|
|
anything to your URLconf for serving static files in development.
|
2010-11-12 05:43:49 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
Learn more
|
|
|
|
==========
|
|
|
|
|
|
|
|
This document has covered the basics and some common usage patterns. For
|
|
|
|
complete details on all the settings, commands, template tags, and other pieces
|
2011-02-27 19:54:03 +08:00
|
|
|
include in ``django.contrib.staticfiles``, see :doc:`the staticfiles reference
|
2010-10-21 01:57:37 +08:00
|
|
|
</ref/contrib/staticfiles>`.
|