Kaydet (Commit) 6c730da1 authored tarafından Tim Graham's avatar Tim Graham

Fixed #19897 - Updated static files howto.

Thanks Jan Murre, Reinout van Rees and Wim Feijen,
plus Remco Wendt for reviewing.
üst c32fc79a
...@@ -113,7 +113,7 @@ Static files are automatically served by the development server. In ...@@ -113,7 +113,7 @@ Static files are automatically served by the development server. In
production, you must define a :setting:`STATIC_ROOT` directory where production, you must define a :setting:`STATIC_ROOT` directory where
:djadmin:`collectstatic` will copy them. :djadmin:`collectstatic` will copy them.
See :doc:`/howto/static-files` for more information. See :doc:`/howto/static-files/index` for more information.
:setting:`MEDIA_ROOT` and :setting:`MEDIA_URL` :setting:`MEDIA_ROOT` and :setting:`MEDIA_URL`
---------------------------------------------- ----------------------------------------------
......
...@@ -21,7 +21,8 @@ you quickly accomplish common tasks. ...@@ -21,7 +21,8 @@ you quickly accomplish common tasks.
legacy-databases legacy-databases
outputting-csv outputting-csv
outputting-pdf outputting-pdf
static-files static-files/index
static-files/deployment
.. seealso:: .. seealso::
......
======================
Deploying static files
======================
.. seealso::
For an introduction to the use of :mod:`django.contrib.staticfiles`, see
:doc:`/howto/static-files/index`.
.. _staticfiles-production:
Serving static files in production
==================================
The basic outline of putting static files into production is simple: run the
: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. Depending on :setting:`STATICFILES_STORAGE`,
files may need to be moved to a new location manually or the :func:`post_process
<django.contrib.staticfiles.storage.StaticFilesStorage.post_process>` method
of the ``Storage`` class might take care of that.
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 site and your static files from the same server
-----------------------------------------------------------
If you want to serve your static files from the same server that's already
serving your site, the process may look something like:
* Push your code up to the deployment server.
* On the server, run :djadmin:`collectstatic` to copy all the static files
into :setting:`STATIC_ROOT`.
* Configure your web server to serve the files in :setting:`STATIC_ROOT`
under the URL :setting:`STATIC_URL`. For example, here's
:ref:`how to do this with Apache and mod_wsgi <serving-files>`.
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
<http://fabfile.org/>`_.
Below, and in the following sections, we'll show off a few example fabfiles
(i.e. Fabric scripts) that automate these file deployment options. The syntax
of a fabfile is fairly straightforward but won't be covered here; consult
`Fabric's documentation <http://docs.fabfile.org/>`_, for a complete
explanation of the syntax.
So, a fabfile to deploy static files to a couple of web servers might look
something like::
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):
run('./manage.py collectstatic -v0 --noinput')
Serving static files from a dedicated server
--------------------------------------------
Most larger Django sites use a separate Web server -- i.e., one that's not also
running Django -- for serving static files. This server often runs a different
type of web server -- faster but less full-featured. Some common choices are:
* lighttpd_
* Nginx_
* TUX_
* Cherokee_
* A stripped-down version of Apache_
.. _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/
Configuring these servers is out of scope of this document; check each
server's respective documentation for instructions.
Since your static file server won't be running Django, you'll need to modify
the deployment strategy to look something like:
* 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 <https://rsync.samba.org/>`_ is a
common choice for this step since it only needs to transfer the bits of
static files that have changed.
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. Your STATIC_ROOT setting.
env.local_static_root = '/tmp/static'
# Where the static files should go remotely
env.remote_static_root = '/home/www/static.example.com'
@roles('static')
def deploy_static():
local('./manage.py collectstatic')
project.rsync_project(
remote_dir = env.remote_static_root,
local_dir = env.local_static_root,
delete = True
)
.. _staticfiles-from-cdn:
Serving static files from a cloud service or CDN
------------------------------------------------
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).
When using these services, the basic workflow would look a bit like the above,
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.
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::
STATICFILES_STORAGE = 'myproject.storage.S3Storage'
Once that's done, all you have to do is run :djadmin:`collectstatic` and your
static files would be pushed through your storage package up to S3. If you
later needed to switch to a different storage provider, it could be as simple
as changing your :setting:`STATICFILES_STORAGE` setting.
For details on how you'd write one of these backends, see
:doc:`/howto/custom-file-storage`. There are 3rd party apps available that
provide storage backends for many common file storage APIs. A good starting
point is the `overview at djangopackages.com
<https://www.djangopackages.com/grids/g/storage-backends/>`_.
Learn more
==========
For complete details on all the settings, commands, template tags, and other
pieces included in :mod:`django.contrib.staticfiles`, see :doc:`the
staticfiles reference </ref/contrib/staticfiles>`.
...@@ -99,6 +99,7 @@ to know about views via the links below: ...@@ -99,6 +99,7 @@ to know about views via the links below:
:doc:`Decorators <topics/http/decorators>` :doc:`Decorators <topics/http/decorators>`
* **Reference:** * **Reference:**
:doc:`Built-in Views <ref/views>` |
:doc:`Request/response objects <ref/request-response>` | :doc:`Request/response objects <ref/request-response>` |
:doc:`TemplateResponse objects <ref/template-response>` :doc:`TemplateResponse objects <ref/template-response>`
...@@ -191,7 +192,7 @@ testing of Django applications: ...@@ -191,7 +192,7 @@ testing of Django applications:
:doc:`Overview <howto/deployment/index>` | :doc:`Overview <howto/deployment/index>` |
:doc:`WSGI servers <howto/deployment/wsgi/index>` | :doc:`WSGI servers <howto/deployment/wsgi/index>` |
:doc:`FastCGI/SCGI/AJP <howto/deployment/fastcgi>` | :doc:`FastCGI/SCGI/AJP <howto/deployment/fastcgi>` |
:doc:`Handling static files <howto/static-files>` | :doc:`Deploying static files <howto/static-files/deployment>` |
:doc:`Tracking code errors by email <howto/error-reporting>` :doc:`Tracking code errors by email <howto/error-reporting>`
The admin The admin
......
...@@ -272,7 +272,7 @@ following blocks." In short, that lets you dramatically cut down on redundancy ...@@ -272,7 +272,7 @@ following blocks." In short, that lets you dramatically cut down on redundancy
in templates: each template has to define only what's unique to that template. in templates: each template has to define only what's unique to that template.
Here's what the "base.html" template, including the use of :doc:`static files Here's what the "base.html" template, including the use of :doc:`static files
</howto/static-files>`, might look like: </howto/static-files/index>`, might look like:
.. code-block:: html+django .. code-block:: html+django
......
...@@ -108,7 +108,7 @@ loaded in the bottom right of the screen. ...@@ -108,7 +108,7 @@ loaded in the bottom right of the screen.
These are the **basics**. For more details on settings and other bits included These are the **basics**. For more details on settings and other bits included
with the framework see with the framework see
:doc:`the static files howto </howto/static-files>` and the :doc:`the static files howto </howto/static-files/index>` and the
:doc:`the staticfiles reference </ref/contrib/staticfiles>`. :doc:`Deploying :doc:`the staticfiles reference </ref/contrib/staticfiles>`. :doc:`Deploying
static files </howto/static-files/deployment>` discusses how to use static static files </howto/static-files/deployment>` discusses how to use static
files on a real server. files on a real server.
......
...@@ -12,7 +12,8 @@ can easily be served in production. ...@@ -12,7 +12,8 @@ can easily be served in production.
.. seealso:: .. seealso::
For an introduction to the static files app and some usage examples, see For an introduction to the static files app and some usage examples, see
:doc:`/howto/static-files`. :doc:`/howto/static-files/index`. For guidelines on deploying static files,
see :doc:`/howto/static-files/deployment`.
.. _staticfiles-settings: .. _staticfiles-settings:
...@@ -326,9 +327,18 @@ files: ...@@ -326,9 +327,18 @@ files:
Static file development view Static file development view
---------------------------- ----------------------------
.. currentmodule:: django.contrib.staticfiles
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.
.. highlight:: python .. highlight:: python
.. function:: django.contrib.staticfiles.views.serve(request, path) .. function:: views.serve(request, path)
This view function serves static files in development. This view function serves static files in development.
...@@ -355,9 +365,10 @@ primary URL configuration:: ...@@ -355,9 +365,10 @@ primary URL configuration::
Note, the beginning of the pattern (``r'^static/'``) should be your Note, the beginning of the pattern (``r'^static/'``) should be your
:setting:`STATIC_URL` setting. :setting:`STATIC_URL` setting.
Since this is a bit finicky, there's also a helper function that'll do this for you: Since this is a bit finicky, there's also a helper function that'll do this for
you:
.. function:: django.contrib.staticfiles.urls.staticfiles_urlpatterns() .. function:: urls.staticfiles_urlpatterns()
This will return the proper URL pattern for serving static files to your This will return the proper URL pattern for serving static files to your
already defined pattern list. Use it like this:: already defined pattern list. Use it like this::
...@@ -368,8 +379,18 @@ already defined pattern list. Use it like this:: ...@@ -368,8 +379,18 @@ already defined pattern list. Use it like this::
urlpatterns += staticfiles_urlpatterns() urlpatterns += staticfiles_urlpatterns()
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 in addition to
files in app directories.
.. warning:: .. warning::
This helper function will only work if :setting:`DEBUG` is ``True`` This helper function will only work if :setting:`DEBUG` is ``True``
and your :setting:`STATIC_URL` setting is neither empty nor a full and your :setting:`STATIC_URL` setting is neither empty nor a full
URL such as ``http://static.example.com/``. URL such as ``http://static.example.com/``.
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**.
...@@ -761,7 +761,8 @@ Serving static files with the development server ...@@ -761,7 +761,8 @@ Serving static files with the development server
By default, the development server doesn't serve any static files for your site By default, the development server doesn't serve any static files for your site
(such as CSS files, images, things under :setting:`MEDIA_URL` and so forth). If (such as CSS files, images, things under :setting:`MEDIA_URL` and so forth). If
you want to configure Django to serve static media, read :doc:`/howto/static-files`. you want to configure Django to serve static media, read
:doc:`/howto/static-files/index`.
shell shell
----- -----
...@@ -1289,7 +1290,7 @@ collectstatic ...@@ -1289,7 +1290,7 @@ collectstatic
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
This command is only available if the :doc:`static files application This command is only available if the :doc:`static files application
</howto/static-files>` (``django.contrib.staticfiles``) is installed. </howto/static-files/index>` (``django.contrib.staticfiles``) is installed.
Please refer to its :djadmin:`description <collectstatic>` in the Please refer to its :djadmin:`description <collectstatic>` in the
:doc:`staticfiles </ref/contrib/staticfiles>` documentation. :doc:`staticfiles </ref/contrib/staticfiles>` documentation.
...@@ -1298,7 +1299,7 @@ findstatic ...@@ -1298,7 +1299,7 @@ findstatic
~~~~~~~~~~ ~~~~~~~~~~
This command is only available if the :doc:`static files application This command is only available if the :doc:`static files application
</howto/static-files>` (``django.contrib.staticfiles``) is installed. </howto/static-files/index>` (``django.contrib.staticfiles``) is installed.
Please refer to its :djadmin:`description <findstatic>` in the :doc:`staticfiles Please refer to its :djadmin:`description <findstatic>` in the :doc:`staticfiles
</ref/contrib/staticfiles>` documentation. </ref/contrib/staticfiles>` documentation.
......
...@@ -25,3 +25,4 @@ API Reference ...@@ -25,3 +25,4 @@ API Reference
urls urls
utils utils
validators validators
views
...@@ -2437,7 +2437,7 @@ Example: ``"/var/www/example.com/static/"`` ...@@ -2437,7 +2437,7 @@ Example: ``"/var/www/example.com/static/"``
If the :doc:`staticfiles</ref/contrib/staticfiles>` contrib app is enabled If the :doc:`staticfiles</ref/contrib/staticfiles>` contrib app is enabled
(default) the :djadmin:`collectstatic` management command will collect static (default) the :djadmin:`collectstatic` management command will collect static
files into this directory. See the howto on :doc:`managing static files into this directory. See the howto on :doc:`managing static
files</howto/static-files>` for more details about usage. files</howto/static-files/index>` for more details about usage.
.. warning:: .. warning::
......
...@@ -2418,8 +2418,10 @@ slightly different call:: ...@@ -2418,8 +2418,10 @@ slightly different call::
The :mod:`staticfiles<django.contrib.staticfiles>` contrib app also ships The :mod:`staticfiles<django.contrib.staticfiles>` contrib app also ships
with a :ttag:`static template tag<staticfiles-static>` which uses with a :ttag:`static template tag<staticfiles-static>` which uses
``staticfiles'`` :setting:`STATICFILES_STORAGE` to build the URL of the ``staticfiles'`` :setting:`STATICFILES_STORAGE` to build the URL of the
given path. Use that instead if you have an advanced use case such as given path (rather than simply using :func:`urlparse.urljoin` with the
:ref:`using a cloud service to serve static files<staticfiles-from-cdn>`:: :setting:`STATIC_URL` setting and the given path). Use that instead if you
have an advanced use case such as :ref:`using a cloud service to serve
static files<staticfiles-from-cdn>`::
{% load static from staticfiles %} {% load static from staticfiles %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" /> <img src="{% static "images/hi.jpg" %}" alt="Hi!" />
......
...@@ -44,6 +44,20 @@ The ``optional_dictionary`` and ``optional_name`` parameters are described in ...@@ -44,6 +44,20 @@ The ``optional_dictionary`` and ``optional_name`` parameters are described in
patterns you can construct. The only limit is that you can only create 254 patterns you can construct. The only limit is that you can only create 254
at a time (the 255th argument is the initial prefix argument). at a time (the 255th argument is the initial prefix argument).
static()
--------
.. function:: static.static(prefix, view='django.views.static.serve', **kwargs)
Helper function to return a URL pattern for serving files in debug mode::
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)
url() url()
----- -----
......
==============
Built-in Views
==============
.. module:: django.views
:synopsis: Django's built-in views.
Several of Django's built-in views are documented in
:doc:`/topics/http/views` as well as elsewhere in the documentation.
Serving files in development
----------------------------
.. function:: static.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. (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`.
``django.contrib.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('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'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.
Since it can become a bit cumbersome to define this URL pattern, Django
ships with a small URL helper function :func:`~django.conf.urls.static.static`
that takes as parameters the prefix 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.
...@@ -72,7 +72,7 @@ at :setting:`STATIC_URL`. ...@@ -72,7 +72,7 @@ at :setting:`STATIC_URL`.
See the :doc:`reference documentation of the app </ref/contrib/staticfiles>` See the :doc:`reference documentation of the app </ref/contrib/staticfiles>`
for more details or learn how to :doc:`manage static files for more details or learn how to :doc:`manage static files
</howto/static-files>`. </howto/static-files/index>`.
``unittest2`` support ``unittest2`` support
~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
......
...@@ -37,7 +37,7 @@ Based on feedback from the community this release adds two new options to the ...@@ -37,7 +37,7 @@ Based on feedback from the community this release adds two new options to the
See the :doc:`staticfiles reference documentation </ref/contrib/staticfiles>` See the :doc:`staticfiles reference documentation </ref/contrib/staticfiles>`
for more details, or learn :doc:`how to manage static files for more details, or learn :doc:`how to manage static files
</howto/static-files>`. </howto/static-files/index>`.
Translation comments Translation comments
~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
......
...@@ -115,7 +115,7 @@ at :setting:`STATIC_URL`. ...@@ -115,7 +115,7 @@ at :setting:`STATIC_URL`.
See the :doc:`reference documentation of the app </ref/contrib/staticfiles>` See the :doc:`reference documentation of the app </ref/contrib/staticfiles>`
for more details or learn how to :doc:`manage static files for more details or learn how to :doc:`manage static files
</howto/static-files>`. </howto/static-files/index>`.
unittest2 support unittest2 support
~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~
......
...@@ -578,7 +578,7 @@ If you've previously used a URL path for ``ADMIN_MEDIA_PREFIX`` (e.g. ...@@ -578,7 +578,7 @@ If you've previously used a URL path for ``ADMIN_MEDIA_PREFIX`` (e.g.
``/media/``) simply make sure :setting:`STATIC_URL` and :setting:`STATIC_ROOT` ``/media/``) simply make sure :setting:`STATIC_URL` and :setting:`STATIC_ROOT`
are configured and your web server serves the files correctly. The development are configured and your web server serves the files correctly. The development
server continues to serve the admin files just like before. Don't hesitate to server continues to serve the admin files just like before. Don't hesitate to
consult the :doc:`static files howto </howto/static-files>` for further consult the :doc:`static files howto </howto/static-files/index>` for further
details. details.
In case your ``ADMIN_MEDIA_PREFIX`` is set to an specific domain (e.g. In case your ``ADMIN_MEDIA_PREFIX`` is set to an specific domain (e.g.
......
...@@ -646,7 +646,7 @@ If you've previously used a URL path for ``ADMIN_MEDIA_PREFIX`` (e.g. ...@@ -646,7 +646,7 @@ If you've previously used a URL path for ``ADMIN_MEDIA_PREFIX`` (e.g.
``/media/``) simply make sure :setting:`STATIC_URL` and :setting:`STATIC_ROOT` ``/media/``) simply make sure :setting:`STATIC_URL` and :setting:`STATIC_ROOT`
are configured and your web server serves the files correctly. The development are configured and your web server serves the files correctly. The development
server continues to serve the admin files just like before. Don't hesitate to server continues to serve the admin files just like before. Don't hesitate to
consult the :doc:`static files howto </howto/static-files>` for further consult the :doc:`static files howto </howto/static-files/index>` for further
details. details.
In case your ``ADMIN_MEDIA_PREFIX`` is set to an specific domain (e.g. In case your ``ADMIN_MEDIA_PREFIX`` is set to an specific domain (e.g.
......
...@@ -708,7 +708,7 @@ If you've previously used a URL path for ``ADMIN_MEDIA_PREFIX`` (e.g. ...@@ -708,7 +708,7 @@ If you've previously used a URL path for ``ADMIN_MEDIA_PREFIX`` (e.g.
``/media/``) simply make sure :setting:`STATIC_URL` and :setting:`STATIC_ROOT` ``/media/``) simply make sure :setting:`STATIC_URL` and :setting:`STATIC_ROOT`
are configured and your Web server serves those files correctly. The are configured and your Web server serves those files correctly. The
development server continues to serve the admin files just like before. Read development server continues to serve the admin files just like before. Read
the :doc:`static files howto </howto/static-files>` for more details. the :doc:`static files howto </howto/static-files/index>` for more details.
If your ``ADMIN_MEDIA_PREFIX`` is set to an specific domain (e.g. If your ``ADMIN_MEDIA_PREFIX`` is set to an specific domain (e.g.
``http://media.example.com/admin/``), make sure to also set your ``http://media.example.com/admin/``), make sure to also set your
......
...@@ -5,7 +5,7 @@ Managing files ...@@ -5,7 +5,7 @@ Managing files
This document describes Django's file access APIs for files such as those This document describes Django's file access APIs for files such as those
uploaded by a user. The lower level APIs are general enough that you could use uploaded by a user. The lower level APIs are general enough that you could use
them for other purposes. If you want to handle "static files" (JS, CSS, etc), them for other purposes. If you want to handle "static files" (JS, CSS, etc),
see :doc:`/howto/static-files`. see :doc:`/howto/static-files/index`.
By default, Django stores files locally, using the :setting:`MEDIA_ROOT` and By default, Django stores files locally, using the :setting:`MEDIA_ROOT` and
:setting:`MEDIA_URL` settings. The examples below assume that you're using these :setting:`MEDIA_URL` settings. The examples below assume that you're using these
......
...@@ -1102,7 +1102,7 @@ out the `full reference`_ for more details. ...@@ -1102,7 +1102,7 @@ out the `full reference`_ for more details.
.. note:: .. note::
``LiveServerTestCase`` makes use of the :doc:`staticfiles contrib app ``LiveServerTestCase`` makes use of the :doc:`staticfiles contrib app
</howto/static-files>` so you'll need to have your project configured </howto/static-files/index>` so you'll need to have your project configured
accordingly (in particular by setting :setting:`STATIC_URL`). accordingly (in particular by setting :setting:`STATIC_URL`).
.. note:: .. note::
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment