summaryrefslogtreecommitdiff
path: root/docs/ref
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2013-03-07 14:15:39 -0500
committerTim Graham <timograham@gmail.com>2013-03-29 19:15:19 -0400
commit6c730da1f6d34f5c38fa1d990d368286e016546c (patch)
treebf4d54de93cd7e33f829b990d3a3eb4564feaef6 /docs/ref
parentc32fc79aa120a0a129680805aef6731c1c2c7aef (diff)
Fixed #19897 - Updated static files howto.
Thanks Jan Murre, Reinout van Rees and Wim Feijen, plus Remco Wendt for reviewing.
Diffstat (limited to 'docs/ref')
-rw-r--r--docs/ref/contrib/staticfiles.txt29
-rw-r--r--docs/ref/django-admin.txt7
-rw-r--r--docs/ref/index.txt1
-rw-r--r--docs/ref/settings.txt2
-rw-r--r--docs/ref/templates/builtins.txt6
-rw-r--r--docs/ref/urls.txt14
-rw-r--r--docs/ref/views.txt48
7 files changed, 97 insertions, 10 deletions
diff --git a/docs/ref/contrib/staticfiles.txt b/docs/ref/contrib/staticfiles.txt
index fa740f4e2c..b7de75baf1 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:
@@ -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**.
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index 6277b22a30..c63c118e3d 100644
--- a/docs/ref/django-admin.txt
+++ b/docs/ref/django-admin.txt
@@ -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.
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 2bfbbe0897..8d7ea6adfb 100644
--- a/docs/ref/settings.txt
+++ b/docs/ref/settings.txt
@@ -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::
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
index 149a557356..0e81df9190 100644
--- a/docs/ref/templates/builtins.txt
+++ b/docs/ref/templates/builtins.txt
@@ -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!" />
diff --git a/docs/ref/urls.txt b/docs/ref/urls.txt
index 59fb97828c..e68edc8254 100644
--- a/docs/ref/urls.txt
+++ b/docs/ref/urls.txt
@@ -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()
-----
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.