diff options
| author | Tim Graham <timograham@gmail.com> | 2013-03-07 14:15:39 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-03-30 07:12:37 -0400 |
| commit | c3779d42142eb83eb8694177a12066261b3243aa (patch) | |
| tree | 0852a96c89f78b28e6bd5463ade8018490d3d7c0 /docs/ref | |
| parent | 1c42a3ec736f55b79bc1a7b4cad4cbe65b9cb27e (diff) | |
[1.5.x] Fixed #19897 - Updated static files howto.
Thanks Jan Murre, Reinout van Rees and Wim Feijen,
plus Remco Wendt for reviewing.
Backport of i6c730da1f from master.
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/contrib/staticfiles.txt | 29 | ||||
| -rw-r--r-- | docs/ref/django-admin.txt | 7 | ||||
| -rw-r--r-- | docs/ref/index.txt | 1 | ||||
| -rw-r--r-- | docs/ref/settings.txt | 8 | ||||
| -rw-r--r-- | docs/ref/templates/builtins.txt | 6 | ||||
| -rw-r--r-- | docs/ref/urls.txt | 14 | ||||
| -rw-r--r-- | docs/ref/views.txt | 48 |
7 files changed, 100 insertions, 13 deletions
diff --git a/docs/ref/contrib/staticfiles.txt b/docs/ref/contrib/staticfiles.txt index f32e1a0f6c..a69a595150 100644 --- a/docs/ref/contrib/staticfiles.txt +++ b/docs/ref/contrib/staticfiles.txt @@ -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: @@ -422,9 +423,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. @@ -451,9 +461,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:: @@ -464,8 +475,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**. diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index d471b59eef..02241ac36c 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -720,7 +720,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 ----- @@ -1235,7 +1236,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. @@ -1244,7 +1245,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. diff --git a/docs/ref/index.txt b/docs/ref/index.txt index fc874a97eb..1d71b62f41 100644 --- a/docs/ref/index.txt +++ b/docs/ref/index.txt @@ -25,3 +25,4 @@ API Reference urls utils validators + views diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index 3a9c368d9f..6f7fc475bc 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -1250,9 +1250,9 @@ see the current list of translated languages by looking in .. _online source: https://github.com/django/django/blob/master/django/conf/global_settings.py -The list is a tuple of two-tuples in the format -(:term:`language code<language code>`, ``language name``) -- for example, -``('ja', 'Japanese')``. +The list is a tuple of two-tuples in the format +(:term:`language code<language code>`, ``language name``) -- for example, +``('ja', 'Japanese')``. This specifies which languages are available for language selection. See :doc:`/topics/i18n/index`. @@ -1912,7 +1912,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:: diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index fadf2db161..a6bba2ad5c 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -2388,8 +2388,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!" /> diff --git a/docs/ref/urls.txt b/docs/ref/urls.txt index d384b0e49b..c0542ef02b 100644 --- a/docs/ref/urls.txt +++ b/docs/ref/urls.txt @@ -52,6 +52,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() ----- diff --git a/docs/ref/views.txt b/docs/ref/views.txt new file mode 100644 index 0000000000..3753f83f07 --- /dev/null +++ b/docs/ref/views.txt @@ -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. |
