Fixed #19897 - Updated static files howto.

Thanks Jan Murre, Reinout van Rees and Wim Feijen,
plus Remco Wendt for reviewing.
This commit is contained in:
Tim Graham 2013-03-07 14:15:39 -05:00
parent c32fc79aa1
commit 6c730da1f6
22 changed files with 340 additions and 481 deletions

View file

@ -12,7 +12,8 @@ can easily be served in production.
.. seealso::
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:
@ -326,9 +327,18 @@ files:
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
.. function:: django.contrib.staticfiles.views.serve(request, path)
.. function:: views.serve(request, path)
This view function serves static files in development.
@ -355,9 +365,10 @@ primary URL configuration::
Note, the beginning of the pattern (``r'^static/'``) should be your
: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
already defined pattern list. Use it like this::
@ -368,8 +379,18 @@ already defined pattern list. Use it like this::
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::
This helper function will only work if :setting:`DEBUG` is ``True``
and your :setting:`STATIC_URL` setting is neither empty nor a full
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**.

View file

@ -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
(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
-----
@ -1289,7 +1290,7 @@ collectstatic
~~~~~~~~~~~~~
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
:doc:`staticfiles </ref/contrib/staticfiles>` documentation.
@ -1298,7 +1299,7 @@ findstatic
~~~~~~~~~~
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
</ref/contrib/staticfiles>` documentation.

View file

@ -25,3 +25,4 @@ API Reference
urls
utils
validators
views

View file

@ -2437,7 +2437,7 @@ Example: ``"/var/www/example.com/static/"``
If the :doc:`staticfiles</ref/contrib/staticfiles>` contrib app is enabled
(default) the :djadmin:`collectstatic` management command will collect 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::

View file

@ -2418,8 +2418,10 @@ slightly different call::
The :mod:`staticfiles<django.contrib.staticfiles>` contrib app also ships
with a :ttag:`static template tag<staticfiles-static>` which uses
``staticfiles'`` :setting:`STATICFILES_STORAGE` to build the URL of 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>`::
given path (rather than simply using :func:`urlparse.urljoin` with the
: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 %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" />

View file

@ -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
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()
-----

48
docs/ref/views.txt Normal file
View file

@ -0,0 +1,48 @@
==============
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.