2013-03-07 20:15:39 +01:00
|
|
|
===================================
|
|
|
|
Managing static files (CSS, images)
|
|
|
|
===================================
|
2005-11-27 15:42:21 +01:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
Websites generally need to serve additional files such as images, JavaScript,
|
|
|
|
or CSS. In Django, we refer to these files as "static files". Django provides
|
|
|
|
:mod:`django.contrib.staticfiles` to help you manage them.
|
2005-11-27 15:42:21 +01:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
This page describes how you can serve these static files.
|
2005-11-27 15:49:57 +01:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
Configuring static files
|
|
|
|
========================
|
2005-11-27 15:42:21 +01:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
1. Make sure that ``django.contrib.staticfiles`` is included in your
|
2011-05-18 11:51:24 +02:00
|
|
|
:setting:`INSTALLED_APPS`.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
2. In your settings file, define :setting:`STATIC_URL`, for example::
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
STATIC_URL = '/static/'
|
2011-03-24 18:52:32 +01:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
3. In your templates, either hardcode the url like
|
|
|
|
``/static/my_app/myexample.jpg`` or, preferably, use the
|
|
|
|
:ttag:`static<staticfiles-static>` template tag to build the URL for the given
|
|
|
|
relative path by using the configured :setting:`STATICFILES_STORAGE` storage
|
|
|
|
(this makes it much easier when you want to switch to a content delivery
|
|
|
|
network (CDN) for serving static files).
|
2011-03-24 18:52:32 +01:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
.. _staticfiles-in-templates:
|
2011-03-24 18:52:32 +01:00
|
|
|
|
2011-05-18 11:51:24 +02:00
|
|
|
.. code-block:: html+django
|
2005-11-27 15:42:21 +01:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
{% load staticfiles %}
|
|
|
|
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
|
2010-11-11 22:43:49 +01:00
|
|
|
|
2013-04-18 19:40:45 +02:00
|
|
|
4. Store your static files in a folder called ``static`` in your app. For
|
2013-03-07 20:15:39 +01:00
|
|
|
example ``my_app/static/my_app/myimage.jpg``.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2013-05-18 18:05:35 +02:00
|
|
|
.. admonition:: Serving the files
|
|
|
|
|
|
|
|
In addition to these configuration steps, you'll also need to actually
|
|
|
|
serve the static files.
|
|
|
|
|
2013-09-01 06:04:32 +02:00
|
|
|
During development, if you use :mod:`django.contrib.staticfiles`, this will
|
|
|
|
be done automatically by :djadmin:`runserver` when :setting:`DEBUG` is set
|
|
|
|
to ``True`` (see :func:`django.contrib.staticfiles.views.serve`).
|
2013-05-18 18:05:35 +02:00
|
|
|
|
|
|
|
This method is **grossly inefficient** and probably **insecure**,
|
|
|
|
so it is **unsuitable for production**.
|
|
|
|
|
|
|
|
See :doc:`/howto/static-files/deployment` for proper strategies to serve
|
|
|
|
static files in production environments.
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
Your project will probably also have static assets that aren't tied to a
|
|
|
|
particular app. In addition to using a ``static/`` directory inside your apps,
|
|
|
|
you can define a list of directories (:setting:`STATICFILES_DIRS`) in your
|
|
|
|
settings file where Django will also look for static files. For example::
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
STATICFILES_DIRS = (
|
|
|
|
os.path.join(BASE_DIR, "static"),
|
|
|
|
'/var/www/static/',
|
2005-11-27 15:42:21 +01:00
|
|
|
)
|
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
See the documentation for the :setting:`STATICFILES_FINDERS` setting for
|
|
|
|
details on how ``staticfiles`` finds your files.
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
.. admonition:: Static file namespacing
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
Now we *might* be able to get away with putting our static files directly
|
|
|
|
in ``my_app/static/`` (rather than creating another ``my_app``
|
|
|
|
subdirectory), but it would actually be a bad idea. Django will use the
|
2013-06-21 20:55:59 +02:00
|
|
|
first static file it finds whose name matches, and if you had a static file
|
2013-03-07 20:15:39 +01:00
|
|
|
with the same name in a *different* application, Django would be unable to
|
|
|
|
distinguish between them. We need to be able to point Django at the right
|
|
|
|
one, and the easiest way to ensure this is by *namespacing* them. That is,
|
|
|
|
by putting those static files inside *another* directory named for the
|
|
|
|
application itself.
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2014-05-05 13:36:53 +02:00
|
|
|
.. _serving-static-files-in-development:
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-09-01 06:04:32 +02:00
|
|
|
Serving static files during development.
|
|
|
|
========================================
|
|
|
|
|
|
|
|
If you use :mod:`django.contrib.staticfiles` as explained above,
|
|
|
|
:djadmin:`runserver` will do this automatically when :setting:`DEBUG` is set
|
|
|
|
to ``True``. If you don't have ``django.contrib.staticfiles`` in
|
|
|
|
:setting:`INSTALLED_APPS`, you can still manually serve static files using the
|
|
|
|
:func:`django.contrib.staticfiles.views.serve` view.
|
|
|
|
|
|
|
|
This is not suitable for production use! For some common deployment
|
|
|
|
strategies, see :doc:`/howto/static-files/deployment`.
|
|
|
|
|
|
|
|
For example, if your :setting:`STATIC_URL` is defined as ``/static/``, you can do
|
|
|
|
this by adding the following snippet to your urls.py::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.conf.urls.static import static
|
|
|
|
|
2014-04-02 02:46:34 +02:00
|
|
|
urlpatterns = [
|
2013-09-01 06:04:32 +02:00
|
|
|
# ... the rest of your URLconf goes here ...
|
2014-04-02 02:46:34 +02:00
|
|
|
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
2013-09-01 06:04:32 +02:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
This helper function works only in debug mode and only if
|
|
|
|
the given prefix is local (e.g. ``/static/``) and not a URL (e.g.
|
|
|
|
``http://static.example.com/``).
|
|
|
|
|
|
|
|
Also this helper function only serves the actual :setting:`STATIC_ROOT`
|
|
|
|
folder; it doesn't perform static files discovery like
|
2013-09-10 22:10:00 +02:00
|
|
|
:mod:`django.contrib.staticfiles`.
|
2013-09-01 06:04:32 +02:00
|
|
|
|
2014-05-05 13:36:53 +02:00
|
|
|
.. _serving-uploaded-files-in-development:
|
|
|
|
|
2013-09-01 06:04:32 +02:00
|
|
|
Serving files uploaded by a user during development.
|
|
|
|
====================================================
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
During development, you can serve user-uploaded media files from
|
|
|
|
:setting:`MEDIA_ROOT` using the :func:`django.contrib.staticfiles.views.serve`
|
2013-09-01 06:04:32 +02:00
|
|
|
view.
|
|
|
|
|
|
|
|
This is not suitable for production use! For some common deployment
|
2013-03-07 20:15:39 +01:00
|
|
|
strategies, see :doc:`/howto/static-files/deployment`.
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-09-01 06:04:32 +02:00
|
|
|
For example, if your :setting:`MEDIA_URL` is defined as ``/media/``, you can do
|
2013-03-07 20:15:39 +01:00
|
|
|
this by adding the following snippet to your urls.py::
|
2011-02-14 02:42:26 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.conf.urls.static import static
|
|
|
|
|
2014-04-02 02:46:34 +02:00
|
|
|
urlpatterns = [
|
2011-02-14 02:42:26 +01:00
|
|
|
# ... the rest of your URLconf goes here ...
|
2014-04-02 02:46:34 +02:00
|
|
|
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2011-03-15 21:13:21 +01:00
|
|
|
.. note::
|
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
This helper function works only in debug mode and only if
|
2013-09-01 06:04:32 +02:00
|
|
|
the given prefix is local (e.g. ``/media/``) and not a URL (e.g.
|
|
|
|
``http://media.example.com/``).
|
2011-03-15 21:13:21 +01:00
|
|
|
|
2013-06-01 19:24:46 +02:00
|
|
|
.. _staticfiles-testing-support:
|
|
|
|
|
|
|
|
Testing
|
|
|
|
=======
|
|
|
|
|
|
|
|
When running tests that use actual HTTP requests instead of the built-in
|
|
|
|
testing client (i.e. when using the built-in :class:`LiveServerTestCase
|
|
|
|
<django.test.LiveServerTestCase>`) the static assets need to be served along
|
|
|
|
the rest of the content so the test environment reproduces the real one as
|
|
|
|
faithfully as possible, but ``LiveServerTestCase`` has only very basic static
|
|
|
|
file-serving functionality: It doesn't know about the finders feature of the
|
|
|
|
``staticfiles`` application and assumes the static content has already been
|
|
|
|
collected under :setting:`STATIC_ROOT`.
|
|
|
|
|
|
|
|
Because of this, ``staticfiles`` ships its own
|
2014-08-28 08:44:26 +02:00
|
|
|
:class:`django.contrib.staticfiles.testing.StaticLiveServerTestCase`, a subclass
|
2013-06-01 19:24:46 +02:00
|
|
|
of the built-in one that has the ability to transparently serve all the assets
|
|
|
|
during execution of these tests in a way very similar to what we get at
|
|
|
|
development time with ``DEBUG = True``, i.e. without having to collect them
|
|
|
|
using :djadmin:`collectstatic` first.
|
|
|
|
|
|
|
|
.. versionadded:: 1.7
|
|
|
|
|
2014-08-28 08:44:26 +02:00
|
|
|
:class:`django.contrib.staticfiles.testing.StaticLiveServerTestCase` is new
|
|
|
|
in Django 1.7. Previously its functionality was provided by
|
2013-06-01 19:24:46 +02:00
|
|
|
:class:`django.test.LiveServerTestCase`.
|
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
Deployment
|
|
|
|
==========
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
:mod:`django.contrib.staticfiles` provides a convenience management command
|
|
|
|
for gathering static files in a single directory so you can serve them easily.
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
1. Set the :setting:`STATIC_ROOT` setting to the directory from which you'd
|
|
|
|
like to serve these files, for example::
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
STATIC_ROOT = "/var/www/example.com/static/"
|
2010-10-20 03:33:24 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
2. Run the :djadmin:`collectstatic` management command::
|
2010-10-26 14:32:35 +02:00
|
|
|
|
2013-09-18 16:35:41 +02:00
|
|
|
$ python manage.py collectstatic
|
2010-10-26 14:32:35 +02:00
|
|
|
|
2013-03-07 20:15:39 +01:00
|
|
|
This will copy all files from your static folders into the
|
|
|
|
:setting:`STATIC_ROOT` directory.
|
2010-10-26 14:32:35 +02:00
|
|
|
|
2013-04-29 19:40:03 +02:00
|
|
|
3. Use a web server of your choice to serve the
|
2013-03-07 20:15:39 +01:00
|
|
|
files. :doc:`/howto/static-files/deployment` covers some common deployment
|
|
|
|
strategies for static files.
|
2010-11-11 22:43:49 +01:00
|
|
|
|
2010-10-20 03:33:24 +02: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
|
2013-03-07 20:15:39 +01:00
|
|
|
included in :mod:`django.contrib.staticfiles`, see :doc:`the staticfiles
|
|
|
|
reference </ref/contrib/staticfiles>`.
|