1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #21221 -- Made form Media and static template tag use staticfiles if installed.

This commit is contained in:
Johannes Hoppe
2015-11-07 12:24:38 +01:00
committed by Tim Graham
parent 6be9589eb3
commit cf546e11ac
34 changed files with 145 additions and 144 deletions

View File

@@ -19,17 +19,17 @@ Configuring static files
STATIC_URL = '/static/'
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).
``/static/my_app/myexample.jpg`` or, preferably, use the :ttag:`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).
.. _staticfiles-in-templates:
.. code-block:: html+django
{% load staticfiles %}
{% load static %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
4. Store your static files in a folder called ``static`` in your app. For

View File

@@ -304,7 +304,7 @@ Here's what the "base.html" template, including the use of :doc:`static files
.. snippet:: html+django
:filename: mysite/templates/base.html
{% load staticfiles %}
{% load static %}
<html>
<head>
<title>{% block title %}{% endblock %}</title>

View File

@@ -68,13 +68,11 @@ Next, add the following at the top of ``polls/templates/polls/index.html``:
.. snippet:: html+django
:filename: polls/templates/polls/index.html
{% load staticfiles %}
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
``{% load staticfiles %}`` loads the :ttag:`{% static %} <staticfiles-static>`
template tag from the ``staticfiles`` template library. The ``{% static %}``
template tag generates the absolute URL of the static file.
The ``{% static %}`` template tag generates the absolute URL of static files.
That's all you need to do for development. Reload
``http://localhost:8000/polls/`` and you should see that the question links are

View File

@@ -26,7 +26,7 @@ In your custom ``change_form.html`` template, extend the
.. code-block:: html+django
{% extends 'admin/change_form.html' %}
{% load admin_static %}
{% load static %}
{% block admin_change_form_document_ready %}
{{ block.super }}
@@ -65,7 +65,7 @@ namespace, just listen to the event triggered from there. For example:
.. code-block:: html+django
{% extends 'admin/change_form.html' %}
{% load admin_static %}
{% load static %}
{% block admin_change_form_document_ready %}
{{ block.super }}

View File

@@ -285,11 +285,16 @@ following requirements are met:
* the :setting:`STATICFILES_STORAGE` setting is set to
``'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'``
* the :setting:`DEBUG` setting is set to ``False``
* you use the ``staticfiles`` :ttag:`static<staticfiles-static>` template
tag to refer to your static files in your templates
* you've collected all your static files by using the
:djadmin:`collectstatic` management command
.. versionchanged:: 1.10
In older versions, you also had to use
``{% load static from staticfiles %}`` in your template. The
:ttag:`static` template tag (``{% load static %}``) now uses
:mod:`django.contrib.staticfiles` if it's installed.
Since creating the MD5 hash can be a performance burden to your website
during runtime, ``staticfiles`` will automatically store the mapping with
hashed names for all processed files in a file called ``staticfiles.json``.
@@ -331,43 +336,6 @@ If you want to override certain options of the cache backend the storage uses,
simply specify a custom entry in the :setting:`CACHES` setting named
``'staticfiles'``. It falls back to using the ``'default'`` cache backend.
.. currentmodule:: django.contrib.staticfiles.templatetags.staticfiles
Template tags
=============
static
------
.. templatetag:: staticfiles-static
Uses the configured :setting:`STATICFILES_STORAGE` storage to create the
full URL for the given relative path, e.g.:
.. code-block:: html+django
{% load static from staticfiles %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" />
The previous example is equal to calling the ``url`` method of an instance of
:setting:`STATICFILES_STORAGE` with ``"images/hi.jpg"``. This is especially
useful when using a non-local storage backend to deploy files as documented
in :ref:`staticfiles-from-cdn`.
If you'd like to retrieve a static URL without displaying it, you can use a
slightly different call:
.. code-block:: html+django
{% load static from staticfiles %}
{% static "images/hi.jpg" as myphoto %}
<img src="{{ myphoto }}" alt="Hi!" />
.. admonition:: Using Jinja2 templates?
See :class:`django.template.backends.jinja2.Jinja2` for information on
using the ``static`` tag with Jinja2.
Finders Module
==============
@@ -390,8 +358,10 @@ files:
which adds :setting:`STATIC_URL` to every template context rendered
with :class:`~django.template.RequestContext` contexts.
- The builtin template tag :ttag:`static` which takes a path and
urljoins it with the static prefix :setting:`STATIC_URL`.
- The builtin template tag :ttag:`static` which takes a path and urljoins it
with the static prefix :setting:`STATIC_URL`. If
``django.contrib.staticfiles`` is installed, the tag uses the ``url()``
method of the :setting:`STATICFILES_STORAGE` instead.
- The builtin template tag :ttag:`get_static_prefix` which populates a
template variable with the static prefix :setting:`STATIC_URL` to be

View File

@@ -2399,8 +2399,9 @@ static
""""""
To link to static files that are saved in :setting:`STATIC_ROOT` Django ships
with a :ttag:`static` template tag. You can use this regardless if you're
using :class:`~django.template.RequestContext` or not. For example::
with a :ttag:`static` template tag. If the :mod:`django.contrib.staticfiles`
app is installed, the tag will serve files using ``url()`` method of the
storage specified by :setting:`STATICFILES_STORAGE`. For example::
{% load static %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" />
@@ -2418,18 +2419,16 @@ slightly different call::
{% static "images/hi.jpg" as myphoto %}
<img src="{{ myphoto }}"></img>
.. note::
.. admonition:: Using Jinja2 templates?
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 (rather than simply using :func:`urllib.parse.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>`::
See :class:`~django.template.backends.jinja2.Jinja2` for information on
using the ``static`` tag with Jinja2.
{% load static from staticfiles %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" />
.. versionchanged:: 1.10
In older versions, you had to use ``{% load static from staticfiles %}`` in
your template to serve files from the storage defined in
:setting:`STATICFILES_STORAGE`. This is no longer required.
.. templatetag:: get_static_prefix

View File

@@ -127,7 +127,11 @@ Minor features
:mod:`django.contrib.staticfiles`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* ...
* The :ttag:`static` template tag now uses ``django.contrib.staticfiles``
if it's in ``INSTALLED_APPS``. This is especially useful for third-party apps
which can now always use ``{% load static %}`` (instead of
``{% load staticfiles %}`` or ``{% load static from staticfiles %}``) and
not worry about whether or not the ``staticfiles`` app is installed.
:mod:`django.contrib.syndication`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -169,7 +173,8 @@ File Uploads
Forms
^^^^^
* ...
* Form and widget ``Media`` is now served using
:mod:`django.contrib.staticfiles` if installed.
Generic Views
^^^^^^^^^^^^^

View File

@@ -459,10 +459,10 @@ more details.
~~~~~~~~~~~~~~~~~~~~~~~
The :mod:`staticfiles<django.contrib.staticfiles>` contrib app has a new
:ttag:`static<staticfiles-static>` template tag to refer to files saved with
the :setting:`STATICFILES_STORAGE` storage backend. It uses the storage
backend's ``url`` method and therefore supports advanced features such as
:ref:`serving files from a cloud service<staticfiles-from-cdn>`.
``static`` template tag to refer to files saved with the
:setting:`STATICFILES_STORAGE` storage backend. It uses the storage backend's
``url`` method and therefore supports advanced features such as :ref:`serving
files from a cloud service<staticfiles-from-cdn>`.
``CachedStaticFilesStorage`` storage backend
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -203,12 +203,13 @@ Paths in asset definitions
Paths used to specify assets can be either relative or absolute. If a
path starts with ``/``, ``http://`` or ``https://``, it will be
interpreted as an absolute path, and left as-is. All other paths will
be prepended with the value of the appropriate prefix.
be prepended with the value of the appropriate prefix. If the
:mod:`django.contrib.staticfiles` app is installed, it will be used to serve
assets.
As part of the introduction of the
:doc:`staticfiles app </ref/contrib/staticfiles>` two new settings were added
to refer to "static files" (images, CSS, JavaScript, etc.) that are needed
to render a complete web page: :setting:`STATIC_URL` and :setting:`STATIC_ROOT`.
Whether or not you use :mod:`django.contrib.staticfiles`, the
:setting:`STATIC_URL` and :setting:`STATIC_ROOT` settings are required to
render a complete web page.
To find the appropriate prefix to use, Django will check if the
:setting:`STATIC_URL` setting is not ``None`` and automatically fall back
@@ -238,6 +239,18 @@ But if :setting:`STATIC_URL` is ``'http://static.example.com/'``::
<script type="text/javascript" src="http://static.example.com/animations.js"></script>
<script type="text/javascript" src="http://othersite.com/actions.js"></script>
Or if :mod:`~django.contrib.staticfiles` is configured using the
`~django.contib.staticfiles.ManifestStaticFilesStorage`::
>>> w = CalendarWidget()
>>> print(w.media)
<link href="/css/pretty.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="https://static.example.com/animations.27e20196a850.js"></script>
<script type="text/javascript" src="http://othersite.com/actions.js"></script>
.. versionchanged:: 1.10
Older versions didn't serve assets using :mod:`django.contrib.staticfiles`.
``Media`` objects
-----------------