2010-05-09 05:38:27 +08:00
|
|
|
============
|
|
|
|
Django Utils
|
|
|
|
============
|
|
|
|
|
|
|
|
.. module:: django.utils
|
|
|
|
:synopsis: Django's built-in utilities.
|
|
|
|
|
|
|
|
This document covers all stable modules in ``django.utils``. Most of the
|
|
|
|
modules in ``django.utils`` are designed for internal use and only the
|
|
|
|
following parts can be considered stable and thus backwards compatible as per
|
|
|
|
the :ref:`internal release deprecation policy <internal-release-deprecation-policy>`.
|
|
|
|
|
|
|
|
``django.utils.cache``
|
|
|
|
======================
|
|
|
|
|
|
|
|
.. module:: django.utils.cache
|
|
|
|
:synopsis: Helper functions for controlling caching.
|
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
This module contains helper functions for controlling HTTP caching. It does so
|
|
|
|
by managing the ``Vary`` header of responses. It includes functions to patch
|
|
|
|
the header of response objects directly and decorators that change functions to
|
|
|
|
do that header-patching themselves.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2016-05-02 20:35:05 +08:00
|
|
|
For information on the ``Vary`` header, see :rfc:`7231#section-7.1.4`.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
Essentially, the ``Vary`` HTTP header defines which headers a cache should take
|
|
|
|
into account when building its cache key. Requests with the same path but
|
|
|
|
different header content for headers named in ``Vary`` need to get different
|
|
|
|
cache keys to prevent delivery of wrong content.
|
|
|
|
|
2014-03-02 23:00:30 +08:00
|
|
|
For example, :doc:`internationalization </topics/i18n/index>` middleware would
|
|
|
|
need to distinguish caches by the ``Accept-language`` header.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: patch_cache_control(response, **kwargs)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
This function patches the ``Cache-Control`` header by adding all keyword
|
|
|
|
arguments to it. The transformation is as follows:
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* All keyword parameter names are turned to lowercase, and underscores
|
|
|
|
are converted to hyphens.
|
|
|
|
* If the value of a parameter is ``True`` (exactly ``True``, not just a
|
|
|
|
true value), only the parameter name is added to the header.
|
|
|
|
* All other parameters are added with their value, after applying
|
|
|
|
``str()`` to it.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2019-09-27 04:58:14 +08:00
|
|
|
.. versionchanged:: 3.1
|
|
|
|
|
|
|
|
Support for multiple field names in the ``no-cache`` directive was
|
|
|
|
added.
|
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
.. function:: get_max_age(response)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Returns the max-age from the response Cache-Control header as an integer
|
|
|
|
(or ``None`` if it wasn't found or wasn't an integer).
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: patch_response_headers(response, cache_timeout=None)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Adds some useful headers to the given ``HttpResponse`` object:
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``Expires``
|
|
|
|
* ``Cache-Control``
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Each header is only added if it isn't already set.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-05-30 01:41:04 +08:00
|
|
|
``cache_timeout`` is in seconds. The :setting:`CACHE_MIDDLEWARE_SECONDS`
|
|
|
|
setting is used by default.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: add_never_cache_headers(response)
|
|
|
|
|
2019-06-25 14:15:00 +08:00
|
|
|
Adds a ``Cache-Control: max-age=0, no-cache, no-store, must-revalidate,
|
|
|
|
private`` header to a response to indicate that a page should never be
|
|
|
|
cached.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.0
|
|
|
|
|
|
|
|
``private`` directive was added.
|
2015-04-28 05:56:02 +08:00
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
.. function:: patch_vary_headers(response, newheaders)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Adds (or updates) the ``Vary`` header in the given ``HttpResponse`` object.
|
2019-08-14 04:40:09 +08:00
|
|
|
``newheaders`` is a list of header names that should be in ``Vary``. If
|
|
|
|
headers contains an asterisk, then ``Vary`` header will consist of a single
|
|
|
|
asterisk ``'*'``. Otherwise, existing headers in ``Vary`` aren't removed.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.0
|
|
|
|
|
|
|
|
Handling an asterisk ``'*'`` according to :rfc:`7231#section-7.1.4` was
|
|
|
|
added.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: get_cache_key(request, key_prefix=None)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Returns a cache key based on the request path. It can be used in the
|
|
|
|
request phase because it pulls the list of headers to take into account
|
|
|
|
from the global path registry and uses those to build a cache key to
|
|
|
|
check against.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
If there is no headerlist stored, the page needs to be rebuilt, so this
|
|
|
|
function returns ``None``.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: learn_cache_key(request, response, cache_timeout=None, key_prefix=None)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Learns what headers to take into account for some request path from the
|
|
|
|
response object. It stores those headers in a global path registry so that
|
|
|
|
later access to that path will know what headers to take into account
|
|
|
|
without building the response object itself. The headers are named in
|
|
|
|
the ``Vary`` header of the response, but we want to prevent response
|
|
|
|
generation.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
The list of headers to use for cache key generation is stored in the same
|
|
|
|
cache as the pages themselves. If the cache ages some data out of the
|
2019-06-17 22:54:55 +08:00
|
|
|
cache, this means that we have to build the response once to get at the
|
|
|
|
Vary header and so at the list of headers to use for the cache key.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
``django.utils.dateparse``
|
|
|
|
==========================
|
|
|
|
|
|
|
|
.. module:: django.utils.dateparse
|
2018-09-15 04:55:17 +08:00
|
|
|
:synopsis: Functions to parse strings to datetime objects.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
|
|
|
The functions defined in this module share the following properties:
|
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
- They accept strings in ISO 8601 date/time formats (or some close
|
|
|
|
alternatives) and return objects from the corresponding classes in Python's
|
|
|
|
:mod:`datetime` module.
|
2014-04-26 22:00:15 +08:00
|
|
|
- They raise :exc:`ValueError` if their input is well formatted but isn't a
|
|
|
|
valid date or time.
|
2011-11-18 21:01:06 +08:00
|
|
|
- They return ``None`` if it isn't well formatted at all.
|
|
|
|
- They accept up to picosecond resolution in input, but they truncate it to
|
|
|
|
microseconds, since that's what Python supports.
|
|
|
|
|
|
|
|
.. function:: parse_date(value)
|
|
|
|
|
|
|
|
Parses a string and returns a :class:`datetime.date`.
|
|
|
|
|
|
|
|
.. function:: parse_time(value)
|
|
|
|
|
|
|
|
Parses a string and returns a :class:`datetime.time`.
|
|
|
|
|
|
|
|
UTC offsets aren't supported; if ``value`` describes one, the result is
|
|
|
|
``None``.
|
|
|
|
|
2019-09-26 04:17:22 +08:00
|
|
|
.. versionchanged:: 3.1
|
|
|
|
|
|
|
|
Support for comma separators for milliseconds was added.
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
.. function:: parse_datetime(value)
|
|
|
|
|
|
|
|
Parses a string and returns a :class:`datetime.datetime`.
|
|
|
|
|
|
|
|
UTC offsets are supported; if ``value`` describes one, the result's
|
2018-07-10 04:33:36 +08:00
|
|
|
``tzinfo`` attribute is a :class:`datetime.timezone` instance.
|
|
|
|
|
2019-09-26 04:17:22 +08:00
|
|
|
.. versionchanged:: 3.1
|
|
|
|
|
|
|
|
Support for comma separators for milliseconds was added.
|
|
|
|
|
2014-07-24 20:57:24 +08:00
|
|
|
.. function:: parse_duration(value)
|
|
|
|
|
|
|
|
Parses a string and returns a :class:`datetime.timedelta`.
|
|
|
|
|
2019-09-26 04:17:22 +08:00
|
|
|
Expects data in the format ``"DD HH:MM:SS.uuuuuu"``,
|
|
|
|
``"DD HH:MM:SS,uuuuuu"``, or as specified by ISO 8601 (e.g.
|
|
|
|
``P4DT1H15M20S`` which is equivalent to ``4 1:15:20``) or PostgreSQL's
|
|
|
|
day-time interval format (e.g. ``3 days 04:05:06``).
|
2017-04-13 10:03:35 +08:00
|
|
|
|
2019-11-27 15:14:00 +08:00
|
|
|
.. versionchanged:: 3.1
|
|
|
|
|
|
|
|
Support for comma separators for decimal fractions in the ISO 8601
|
2019-09-26 04:17:22 +08:00
|
|
|
format and for the format ``"DD HH:MM:SS,uuuuuu"`` was added.
|
2019-11-27 15:14:00 +08:00
|
|
|
|
2012-10-01 01:37:25 +08:00
|
|
|
``django.utils.decorators``
|
|
|
|
===========================
|
|
|
|
|
|
|
|
.. module:: django.utils.decorators
|
|
|
|
:synopsis: Functions that help with creating decorators for views.
|
|
|
|
|
2015-07-22 04:54:37 +08:00
|
|
|
.. function:: method_decorator(decorator, name='')
|
2012-10-01 01:37:25 +08:00
|
|
|
|
2015-07-22 04:54:37 +08:00
|
|
|
Converts a function decorator into a method decorator. It can be used to
|
|
|
|
decorate methods or classes; in the latter case, ``name`` is the name
|
2015-08-11 19:35:50 +08:00
|
|
|
of the method to be decorated and is required.
|
|
|
|
|
2016-03-14 02:47:58 +08:00
|
|
|
``decorator`` may also be a list or tuple of functions. They are wrapped
|
2015-08-11 19:35:50 +08:00
|
|
|
in reverse order so that the call order is the order in which the functions
|
|
|
|
appear in the list/tuple.
|
|
|
|
|
|
|
|
See :ref:`decorating class based views <decorating-class-based-views>` for
|
|
|
|
example usage.
|
2012-10-01 01:37:25 +08:00
|
|
|
|
|
|
|
.. function:: decorator_from_middleware(middleware_class)
|
|
|
|
|
|
|
|
Given a middleware class, returns a view decorator. This lets you use
|
|
|
|
middleware functionality on a per-view basis. The middleware is created
|
|
|
|
with no params passed.
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
It assumes middleware that's compatible with the old style of Django 1.9
|
|
|
|
and earlier (having methods like ``process_request()``,
|
|
|
|
``process_exception()``, and ``process_response()``).
|
|
|
|
|
2012-10-01 01:37:25 +08:00
|
|
|
.. function:: decorator_from_middleware_with_args(middleware_class)
|
|
|
|
|
|
|
|
Like ``decorator_from_middleware``, but returns a function
|
|
|
|
that accepts the arguments to be passed to the middleware_class.
|
|
|
|
For example, the :func:`~django.views.decorators.cache.cache_page`
|
2013-01-01 21:12:42 +08:00
|
|
|
decorator is created from the ``CacheMiddleware`` like this::
|
2012-10-01 01:37:25 +08:00
|
|
|
|
|
|
|
cache_page = decorator_from_middleware_with_args(CacheMiddleware)
|
|
|
|
|
|
|
|
@cache_page(3600)
|
|
|
|
def my_view(request):
|
|
|
|
pass
|
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
``django.utils.encoding``
|
|
|
|
=========================
|
|
|
|
|
|
|
|
.. module:: django.utils.encoding
|
2013-06-29 17:52:29 +08:00
|
|
|
:synopsis: A series of helper functions to manage character encoding.
|
2012-07-21 16:00:10 +08:00
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
.. function:: smart_str(s, encoding='utf-8', strings_only=False, errors='strict')
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
Returns a ``str`` object representing arbitrary object ``s``. Treats
|
|
|
|
bytestrings using the ``encoding`` codec.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
If ``strings_only`` is ``True``, don't convert (some) non-string-like
|
|
|
|
objects.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: is_protected_type(obj)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Determine if the object instance is of a protected type.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Objects of protected types are preserved as-is when passed to
|
2017-01-26 17:08:08 +08:00
|
|
|
``force_str(strings_only=True)``.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
.. function:: force_str(s, encoding='utf-8', strings_only=False, errors='strict')
|
2012-07-21 16:00:10 +08:00
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
Similar to ``smart_str()``, except that lazy instances are resolved to
|
2011-02-16 09:56:53 +08:00
|
|
|
strings, rather than kept as lazy objects.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
If ``strings_only`` is ``True``, don't convert (some) non-string-like
|
|
|
|
objects.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2012-07-21 16:00:10 +08:00
|
|
|
.. function:: smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
Returns a bytestring version of arbitrary object ``s``, encoded as
|
|
|
|
specified in ``encoding``.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
If ``strings_only`` is ``True``, don't convert (some) non-string-like
|
|
|
|
objects.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2012-08-18 22:36:55 +08:00
|
|
|
.. function:: force_bytes(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
|
|
|
Similar to ``smart_bytes``, except that lazy instances are resolved to
|
|
|
|
bytestrings, rather than kept as lazy objects.
|
|
|
|
|
|
|
|
If ``strings_only`` is ``True``, don't convert (some) non-string-like
|
|
|
|
objects.
|
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
.. function:: smart_text(s, encoding='utf-8', strings_only=False, errors='strict')
|
2012-07-21 16:00:10 +08:00
|
|
|
|
2019-02-05 22:19:49 +08:00
|
|
|
.. deprecated:: 3.0
|
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
Alias of :func:`force_str` for backwards compatibility, especially in code
|
|
|
|
that supports Python 2.
|
2012-07-21 16:00:10 +08:00
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
.. function:: force_text(s, encoding='utf-8', strings_only=False, errors='strict')
|
2017-01-19 00:51:29 +08:00
|
|
|
|
2019-02-05 22:19:49 +08:00
|
|
|
.. deprecated:: 3.0
|
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
Alias of :func:`force_str` for backwards compatibility, especially in code
|
|
|
|
that supports Python 2.
|
2012-08-18 22:36:55 +08:00
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
.. function:: iri_to_uri(iri)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Convert an Internationalized Resource Identifier (IRI) portion to a URI
|
|
|
|
portion that is suitable for inclusion in a URL.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2017-01-24 00:44:25 +08:00
|
|
|
This is the algorithm from section 3.1 of :rfc:`3987#section-3.1`, slightly
|
|
|
|
simplified since the input is assumed to be a string rather than an
|
|
|
|
arbitrary byte stream.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2017-01-24 00:44:25 +08:00
|
|
|
Takes an IRI (string or UTF-8 bytes) and returns a string containing the
|
|
|
|
encoded result.
|
2014-07-22 20:25:22 +08:00
|
|
|
|
|
|
|
.. function:: uri_to_iri(uri)
|
|
|
|
|
|
|
|
Converts a Uniform Resource Identifier into an Internationalized Resource
|
|
|
|
Identifier.
|
|
|
|
|
|
|
|
This is an algorithm from section 3.2 of :rfc:`3987#section-3.2`.
|
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
Takes a URI in ASCII bytes and returns a string containing the encoded
|
|
|
|
result.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2012-12-17 03:34:41 +08:00
|
|
|
.. function:: filepath_to_uri(path)
|
|
|
|
|
|
|
|
Convert a file system path to a URI portion that is suitable for inclusion
|
2019-10-30 04:15:18 +08:00
|
|
|
in a URL. The path is assumed to be either UTF-8 bytes, string, or a
|
|
|
|
:class:`~pathlib.Path`.
|
2012-12-17 03:34:41 +08:00
|
|
|
|
|
|
|
This method will encode certain characters that would normally be
|
|
|
|
recognized as special characters for URIs. Note that this method does not
|
|
|
|
encode the ' character, as it is a valid character within URIs. See
|
|
|
|
``encodeURIComponent()`` JavaScript function for more details.
|
|
|
|
|
|
|
|
Returns an ASCII string containing the encoded result.
|
|
|
|
|
2019-10-30 04:15:18 +08:00
|
|
|
.. versionchanged:: 3.1
|
|
|
|
|
|
|
|
Support for :class:`pathlib.Path` ``path`` was added.
|
|
|
|
|
2014-10-31 23:43:34 +08:00
|
|
|
.. function:: escape_uri_path(path)
|
|
|
|
|
|
|
|
Escapes the unsafe characters from the path portion of a Uniform Resource
|
|
|
|
Identifier (URI).
|
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
``django.utils.feedgenerator``
|
|
|
|
==============================
|
|
|
|
|
|
|
|
.. module:: django.utils.feedgenerator
|
2014-03-03 21:49:18 +08:00
|
|
|
:synopsis: Syndication feed generation library -- used for generating RSS, etc.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
Sample usage::
|
|
|
|
|
|
|
|
>>> from django.utils import feedgenerator
|
|
|
|
>>> feed = feedgenerator.Rss201rev2Feed(
|
2014-03-23 04:30:49 +08:00
|
|
|
... title="Poynter E-Media Tidbits",
|
|
|
|
... link="http://www.poynter.org/column.asp?id=31",
|
|
|
|
... description="A group Weblog by the sharpest minds in online media/journalism/publishing.",
|
|
|
|
... language="en",
|
2010-05-09 05:38:27 +08:00
|
|
|
... )
|
|
|
|
>>> feed.add_item(
|
|
|
|
... title="Hello",
|
2014-03-23 04:30:49 +08:00
|
|
|
... link="http://www.holovaty.com/test/",
|
2016-05-15 07:06:31 +08:00
|
|
|
... description="Testing.",
|
2010-05-09 05:38:27 +08:00
|
|
|
... )
|
2012-05-05 20:01:38 +08:00
|
|
|
>>> with open('test.rss', 'w') as fp:
|
2012-05-05 22:54:30 +08:00
|
|
|
... feed.write(fp, 'utf-8')
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
For simplifying the selection of a generator use ``feedgenerator.DefaultFeed``
|
|
|
|
which is currently ``Rss201rev2Feed``
|
|
|
|
|
|
|
|
For definitions of the different versions of RSS, see:
|
2015-11-30 00:29:46 +08:00
|
|
|
https://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/02/04/incompatible-rss
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: get_tag_uri(url, date)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Creates a TagURI.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2015-11-30 00:29:46 +08:00
|
|
|
See https://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``SyndicationFeed``
|
|
|
|
-------------------
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. class:: SyndicationFeed
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Base class for all syndication feeds. Subclasses should provide write().
|
2011-05-14 20:28:36 +08:00
|
|
|
|
2015-07-27 20:35:21 +08:00
|
|
|
.. method:: __init__(title, link, description, language=None, author_email=None, author_name=None, author_link=None, subtitle=None, categories=None, feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs)
|
2011-03-04 06:20:54 +08:00
|
|
|
|
|
|
|
Initialize the feed with the given dictionary of metadata, which applies
|
|
|
|
to the entire feed.
|
2011-05-14 20:28:36 +08:00
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
Any extra keyword arguments you pass to ``__init__`` will be stored in
|
|
|
|
``self.feed``.
|
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
All parameters should be strings, except ``categories``, which should
|
|
|
|
be a sequence of strings.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2016-12-31 21:27:32 +08:00
|
|
|
.. method:: add_item(title, link, description, author_email=None, author_name=None, author_link=None, pubdate=None, comments=None, unique_id=None, categories=(), item_copyright=None, ttl=None, updateddate=None, enclosures=None, **kwargs)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
Adds an item to the feed. All args are expected to be strings except
|
|
|
|
``pubdate`` and ``updateddate``, which are ``datetime.datetime``
|
2016-12-31 21:27:32 +08:00
|
|
|
objects, and ``enclosures``, which is a list of ``Enclosure`` instances.
|
2013-07-18 03:20:20 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: num_items()
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: root_attributes()
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Return extra attributes to place on the root (i.e. feed/channel)
|
2011-03-04 06:20:54 +08:00
|
|
|
element. Called from ``write()``.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: add_root_elements(handler)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Add elements in the root (i.e. feed/channel) element.
|
2011-03-04 06:20:54 +08:00
|
|
|
Called from ``write()``.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: item_attributes(item)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Return extra attributes to place on each item (i.e. item/entry)
|
|
|
|
element.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: add_item_elements(handler, item)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Add elements on each item (i.e. item/entry) element.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: write(outfile, encoding)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Outputs the feed in the given encoding to ``outfile``, which is a
|
|
|
|
file-like object. Subclasses should override this.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: writeString(encoding)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Returns the feed in the given encoding as a string.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: latest_post_date()
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2013-07-18 03:20:20 +08:00
|
|
|
Returns the latest ``pubdate`` or ``updateddate`` for all items in the
|
|
|
|
feed. If no items have either of these attributes this returns the
|
2016-05-25 00:50:20 +08:00
|
|
|
current UTC date/time.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``Enclosure``
|
|
|
|
-------------
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. class:: Enclosure
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Represents an RSS enclosure
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``RssFeed``
|
|
|
|
-----------
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. class:: RssFeed(SyndicationFeed)
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``Rss201rev2Feed``
|
|
|
|
------------------
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. class:: Rss201rev2Feed(RssFeed)
|
|
|
|
|
2017-05-20 23:51:21 +08:00
|
|
|
Spec: https://cyber.harvard.edu/rss/rss.html
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``RssUserland091Feed``
|
|
|
|
----------------------
|
2011-03-04 06:20:54 +08:00
|
|
|
|
|
|
|
.. class:: RssUserland091Feed(RssFeed)
|
|
|
|
|
|
|
|
Spec: http://backend.userland.com/rss091
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``Atom1Feed``
|
|
|
|
-------------
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. class:: Atom1Feed(SyndicationFeed)
|
|
|
|
|
2015-11-30 00:29:46 +08:00
|
|
|
Spec: https://tools.ietf.org/html/rfc4287
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-06-15 18:48:13 +08:00
|
|
|
``django.utils.functional``
|
|
|
|
===========================
|
|
|
|
|
|
|
|
.. module:: django.utils.functional
|
|
|
|
:synopsis: Functional programming tools.
|
|
|
|
|
2018-07-06 20:56:13 +08:00
|
|
|
.. class:: cached_property(func, name=None)
|
2013-08-08 01:47:07 +08:00
|
|
|
|
|
|
|
The ``@cached_property`` decorator caches the result of a method with a
|
2013-08-08 20:16:48 +08:00
|
|
|
single ``self`` argument as a property. The cached result will persist
|
|
|
|
as long as the instance does, so if the instance is passed around and the
|
|
|
|
function subsequently invoked, the cached result will be returned.
|
2013-08-08 01:47:07 +08:00
|
|
|
|
|
|
|
Consider a typical case, where a view might need to call a model's method
|
|
|
|
to perform some computation, before placing the model instance into the
|
2014-08-18 22:30:44 +08:00
|
|
|
context, where the template might invoke the method once more::
|
2013-08-08 01:47:07 +08:00
|
|
|
|
|
|
|
# the model
|
|
|
|
class Person(models.Model):
|
|
|
|
|
|
|
|
def friends(self):
|
|
|
|
# expensive computation
|
|
|
|
...
|
|
|
|
return friends
|
|
|
|
|
|
|
|
# in the view:
|
|
|
|
if person.friends():
|
2014-08-18 22:30:44 +08:00
|
|
|
...
|
|
|
|
|
|
|
|
And in the template you would have:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2013-08-08 01:47:07 +08:00
|
|
|
|
|
|
|
{% for friend in person.friends %}
|
|
|
|
|
2013-08-08 20:16:48 +08:00
|
|
|
Here, ``friends()`` will be called twice. Since the instance ``person`` in
|
2017-08-10 23:54:19 +08:00
|
|
|
the view and the template are the same, decorating the ``friends()`` method
|
|
|
|
with ``@cached_property`` can avoid that::
|
2013-08-08 01:47:07 +08:00
|
|
|
|
|
|
|
from django.utils.functional import cached_property
|
|
|
|
|
2017-08-10 23:54:19 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def friends(self):
|
|
|
|
...
|
2013-08-08 01:47:07 +08:00
|
|
|
|
|
|
|
Note that as the method is now a property, in Python code it will need to
|
2018-09-15 04:55:17 +08:00
|
|
|
be accessed appropriately::
|
2013-08-08 01:47:07 +08:00
|
|
|
|
|
|
|
# in the view:
|
|
|
|
if person.friends:
|
2014-08-18 22:30:44 +08:00
|
|
|
...
|
2013-08-08 01:47:07 +08:00
|
|
|
|
2013-08-08 20:16:48 +08:00
|
|
|
The cached value can be treated like an ordinary attribute of the instance::
|
|
|
|
|
|
|
|
# clear it, requiring re-computation next time it's called
|
|
|
|
del person.friends # or delattr(person, "friends")
|
|
|
|
|
|
|
|
# set a value manually, that will persist on the instance until cleared
|
|
|
|
person.friends = ["Huckleberry Finn", "Tom Sawyer"]
|
|
|
|
|
2019-03-30 21:35:00 +08:00
|
|
|
Because of the way the :py:ref:`descriptor protocol
|
|
|
|
<descriptor-invocation>` works, using ``del`` (or ``delattr``) on a
|
2019-03-30 21:29:03 +08:00
|
|
|
``cached_property`` that hasn't been accessed raises ``AttributeError``.
|
|
|
|
|
2013-08-08 20:16:48 +08:00
|
|
|
As well as offering potential performance advantages, ``@cached_property``
|
|
|
|
can ensure that an attribute's value does not change unexpectedly over the
|
|
|
|
life of an instance. This could occur with a method whose computation is
|
2019-06-17 22:54:55 +08:00
|
|
|
based on ``datetime.now()``, or if a change were saved to the database by
|
|
|
|
some other process in the brief interval between subsequent invocations of
|
|
|
|
a method on the same instance.
|
2013-08-08 01:47:07 +08:00
|
|
|
|
2018-06-07 21:03:45 +08:00
|
|
|
You can make cached properties of methods. For example, if you had an
|
|
|
|
expensive ``get_friends()`` method and wanted to allow calling it without
|
|
|
|
retrieving the cached value, you could write::
|
2014-05-24 18:29:31 +08:00
|
|
|
|
|
|
|
friends = cached_property(get_friends, name='friends')
|
|
|
|
|
2018-06-07 21:03:45 +08:00
|
|
|
You only need the ``name`` argument for Python < 3.6 support.
|
|
|
|
|
2014-05-24 18:29:31 +08:00
|
|
|
While ``person.get_friends()`` will recompute the friends on each call, the
|
|
|
|
value of the cached property will persist until you delete it as described
|
|
|
|
above::
|
|
|
|
|
|
|
|
x = person.friends # calls first time
|
|
|
|
y = person.get_friends() # calls again
|
|
|
|
z = person.friends # does not call
|
|
|
|
x is z # is True
|
|
|
|
|
2015-11-07 21:30:20 +08:00
|
|
|
.. function:: keep_lazy(func, *resultclasses)
|
|
|
|
|
2014-03-02 23:00:30 +08:00
|
|
|
Django offers many utility functions (particularly in ``django.utils``)
|
|
|
|
that take a string as their first argument and do something to that string.
|
|
|
|
These functions are used by template filters as well as directly in other
|
|
|
|
code.
|
2011-06-15 18:48:13 +08:00
|
|
|
|
|
|
|
If you write your own similar functions and deal with translations, you'll
|
2014-03-02 23:00:30 +08:00
|
|
|
face the problem of what to do when the first argument is a lazy
|
|
|
|
translation object. You don't want to convert it to a string immediately,
|
|
|
|
because you might be using this function outside of a view (and hence the
|
|
|
|
current thread's locale setting will not be correct).
|
2011-06-15 18:48:13 +08:00
|
|
|
|
2015-11-07 21:30:20 +08:00
|
|
|
For cases like this, use the ``django.utils.functional.keep_lazy()``
|
2011-06-15 18:48:13 +08:00
|
|
|
decorator. It modifies the function so that *if* it's called with a lazy
|
2013-09-21 06:21:49 +08:00
|
|
|
translation as one of its arguments, the function evaluation is delayed
|
|
|
|
until it needs to be converted to a string.
|
2011-06-15 18:48:13 +08:00
|
|
|
|
|
|
|
For example::
|
|
|
|
|
2015-11-07 21:30:20 +08:00
|
|
|
from django.utils.functional import keep_lazy, keep_lazy_text
|
2011-06-15 18:48:13 +08:00
|
|
|
|
|
|
|
def fancy_utility_function(s, ...):
|
|
|
|
# Do some conversion on string 's'
|
|
|
|
...
|
2017-01-19 00:51:29 +08:00
|
|
|
fancy_utility_function = keep_lazy(str)(fancy_utility_function)
|
2011-06-15 18:48:13 +08:00
|
|
|
|
2015-11-07 21:30:20 +08:00
|
|
|
# Or more succinctly:
|
2017-01-19 00:51:29 +08:00
|
|
|
@keep_lazy(str)
|
2015-11-07 21:30:20 +08:00
|
|
|
def fancy_utility_function(s, ...):
|
|
|
|
...
|
|
|
|
|
|
|
|
The ``keep_lazy()`` decorator takes a number of extra arguments (``*args``)
|
|
|
|
specifying the type(s) that the original function can return. A common
|
2019-06-17 22:54:55 +08:00
|
|
|
use case is to have functions that return text. For these, you can pass the
|
|
|
|
``str`` type to ``keep_lazy`` (or use the :func:`keep_lazy_text` decorator
|
|
|
|
described in the next section).
|
2011-06-15 18:48:13 +08:00
|
|
|
|
|
|
|
Using this decorator means you can write your function and assume that the
|
2014-03-02 23:00:30 +08:00
|
|
|
input is a proper string, then add support for lazy translation objects at
|
|
|
|
the end.
|
2011-06-15 18:48:13 +08:00
|
|
|
|
2015-11-07 21:30:20 +08:00
|
|
|
.. function:: keep_lazy_text(func)
|
|
|
|
|
2017-01-19 00:51:29 +08:00
|
|
|
A shortcut for ``keep_lazy(str)(func)``.
|
2015-11-07 21:30:20 +08:00
|
|
|
|
|
|
|
If you have a function that returns text and you want to be able to take
|
2019-06-17 22:54:55 +08:00
|
|
|
lazy arguments while delaying their evaluation, you can use this
|
|
|
|
decorator::
|
2015-11-07 21:30:20 +08:00
|
|
|
|
|
|
|
from django.utils.functional import keep_lazy, keep_lazy_text
|
|
|
|
|
|
|
|
# Our previous example was:
|
2017-01-19 00:51:29 +08:00
|
|
|
@keep_lazy(str)
|
2015-11-07 21:30:20 +08:00
|
|
|
def fancy_utility_function(s, ...):
|
|
|
|
...
|
|
|
|
|
|
|
|
# Which can be rewritten as:
|
|
|
|
@keep_lazy_text
|
|
|
|
def fancy_utility_function(s, ...):
|
|
|
|
...
|
|
|
|
|
2012-06-30 23:41:51 +08:00
|
|
|
``django.utils.html``
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. module:: django.utils.html
|
|
|
|
:synopsis: HTML helper functions
|
|
|
|
|
|
|
|
Usually you should build up HTML using Django's templates to make use of its
|
|
|
|
autoescape mechanism, using the utilities in :mod:`django.utils.safestring`
|
2013-05-23 20:00:17 +08:00
|
|
|
where appropriate. This module provides some additional low level utilities for
|
2012-06-30 23:41:51 +08:00
|
|
|
escaping HTML.
|
|
|
|
|
|
|
|
.. function:: escape(text)
|
|
|
|
|
|
|
|
Returns the given text with ampersands, quotes and angle brackets encoded
|
2018-07-19 02:15:42 +08:00
|
|
|
for use in HTML. The input is first coerced to a string and the output has
|
2012-06-30 23:41:51 +08:00
|
|
|
:func:`~django.utils.safestring.mark_safe` applied.
|
|
|
|
|
2019-04-24 19:30:34 +08:00
|
|
|
.. versionchanged:: 3.0
|
|
|
|
|
|
|
|
In older versions, ``'`` is converted to its decimal code ``'``
|
|
|
|
instead of the equivalent hex code ``'``.
|
|
|
|
|
2012-06-30 23:41:51 +08:00
|
|
|
.. function:: conditional_escape(text)
|
|
|
|
|
2014-03-02 23:00:30 +08:00
|
|
|
Similar to ``escape()``, except that it doesn't operate on pre-escaped
|
|
|
|
strings, so it will not double escape.
|
2011-06-15 18:48:13 +08:00
|
|
|
|
2012-07-01 01:54:38 +08:00
|
|
|
.. function:: format_html(format_string, *args, **kwargs)
|
|
|
|
|
2016-05-09 06:07:43 +08:00
|
|
|
This is similar to :meth:`str.format`, except that it is appropriate for
|
2012-07-01 01:54:38 +08:00
|
|
|
building up HTML fragments. All args and kwargs are passed through
|
2016-05-09 06:07:43 +08:00
|
|
|
:func:`conditional_escape` before being passed to ``str.format()``.
|
2012-07-01 01:54:38 +08:00
|
|
|
|
|
|
|
For the case of building up small HTML fragments, this function is to be
|
2016-05-09 06:07:43 +08:00
|
|
|
preferred over string interpolation using ``%`` or ``str.format()``
|
|
|
|
directly, because it applies escaping to all arguments - just like the
|
|
|
|
template system applies escaping by default.
|
2012-07-01 01:54:38 +08:00
|
|
|
|
2014-08-18 22:30:44 +08:00
|
|
|
So, instead of writing::
|
2012-07-01 01:54:38 +08:00
|
|
|
|
2016-05-15 07:06:31 +08:00
|
|
|
mark_safe("%s <b>%s</b> %s" % (
|
|
|
|
some_html,
|
|
|
|
escape(some_text),
|
|
|
|
escape(some_other_text),
|
|
|
|
))
|
2012-07-01 01:54:38 +08:00
|
|
|
|
2014-08-18 22:30:44 +08:00
|
|
|
You should instead use::
|
2012-07-01 01:54:38 +08:00
|
|
|
|
2014-11-27 08:41:27 +08:00
|
|
|
format_html("{} <b>{}</b> {}",
|
2016-05-15 07:06:31 +08:00
|
|
|
mark_safe(some_html),
|
|
|
|
some_text,
|
|
|
|
some_other_text,
|
|
|
|
)
|
2012-07-01 01:54:38 +08:00
|
|
|
|
|
|
|
This has the advantage that you don't need to apply :func:`escape` to each
|
|
|
|
argument and risk a bug and an XSS vulnerability if you forget one.
|
|
|
|
|
2016-05-09 06:07:43 +08:00
|
|
|
Note that although this function uses ``str.format()`` to do the
|
|
|
|
interpolation, some of the formatting options provided by ``str.format()``
|
2012-07-01 01:54:38 +08:00
|
|
|
(e.g. number formatting) will not work, since all arguments are passed
|
|
|
|
through :func:`conditional_escape` which (ultimately) calls
|
2017-01-26 17:08:08 +08:00
|
|
|
:func:`~django.utils.encoding.force_str` on the values.
|
2012-07-01 01:54:38 +08:00
|
|
|
|
2013-01-25 19:53:40 +08:00
|
|
|
.. function:: format_html_join(sep, format_string, args_generator)
|
|
|
|
|
|
|
|
A wrapper of :func:`format_html`, for the common case of a group of
|
|
|
|
arguments that need to be formatted using the same format string, and then
|
|
|
|
joined using ``sep``. ``sep`` is also passed through
|
|
|
|
:func:`conditional_escape`.
|
|
|
|
|
|
|
|
``args_generator`` should be an iterator that returns the sequence of
|
|
|
|
``args`` that will be passed to :func:`format_html`. For example::
|
|
|
|
|
2016-05-15 07:06:31 +08:00
|
|
|
format_html_join(
|
|
|
|
'\n', "<li>{} {}</li>",
|
|
|
|
((u.first_name, u.last_name) for u in users)
|
|
|
|
)
|
2013-01-25 19:53:40 +08:00
|
|
|
|
2012-08-18 20:53:22 +08:00
|
|
|
.. function:: strip_tags(value)
|
|
|
|
|
2014-03-20 23:50:50 +08:00
|
|
|
Tries to remove anything that looks like an HTML tag from the string, that
|
|
|
|
is anything contained within ``<>``.
|
2014-08-07 21:20:59 +08:00
|
|
|
|
|
|
|
Absolutely NO guarantee is provided about the resulting string being
|
2014-03-20 23:50:50 +08:00
|
|
|
HTML safe. So NEVER mark safe the result of a ``strip_tag`` call without
|
|
|
|
escaping it first, for example with :func:`~django.utils.html.escape`.
|
2012-08-18 20:53:22 +08:00
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
strip_tags(value)
|
|
|
|
|
2014-03-02 23:00:30 +08:00
|
|
|
If ``value`` is ``"<b>Joel</b> <button>is</button> a <span>slug</span>"``
|
2014-03-20 23:50:50 +08:00
|
|
|
the return value will be ``"Joel is a slug"``.
|
|
|
|
|
2015-08-18 22:09:17 +08:00
|
|
|
If you are looking for a more robust solution, take a look at the `bleach
|
2018-04-18 06:19:29 +08:00
|
|
|
<https://pypi.org/project/bleach/>`_ Python library.
|
2012-07-01 01:54:38 +08:00
|
|
|
|
2015-03-19 04:42:59 +08:00
|
|
|
.. function:: html_safe()
|
|
|
|
|
|
|
|
The ``__html__()`` method on a class helps non-Django templates detect
|
|
|
|
classes whose output doesn't require HTML escaping.
|
|
|
|
|
|
|
|
This decorator defines the ``__html__()`` method on the decorated class
|
2017-01-19 00:51:29 +08:00
|
|
|
by wrapping ``__str__()`` in :meth:`~django.utils.safestring.mark_safe`.
|
|
|
|
Ensure the ``__str__()`` method does indeed return text that doesn't
|
|
|
|
require HTML escaping.
|
2015-03-19 04:42:59 +08:00
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
``django.utils.http``
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. module:: django.utils.http
|
|
|
|
:synopsis: HTTP helper functions. (URL encoding, cookie handling, ...)
|
|
|
|
|
2017-10-09 20:46:07 +08:00
|
|
|
.. function:: urlencode(query, doseq=False)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2017-01-26 21:25:15 +08:00
|
|
|
A version of Python's :func:`urllib.parse.urlencode` function that can
|
|
|
|
operate on ``MultiValueDict`` and non-string values.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: http_date(epoch_seconds=None)
|
|
|
|
|
2011-09-05 05:17:30 +08:00
|
|
|
Formats the time to match the :rfc:`1123` date format as specified by HTTP
|
2016-05-02 20:35:05 +08:00
|
|
|
:rfc:`7231#section-7.1.1.1`.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Accepts a floating point number expressed in seconds since the epoch in
|
|
|
|
UTC--such as that outputted by ``time.time()``. If set to ``None``,
|
|
|
|
defaults to the current time.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Outputs a string in the format ``Wdy, DD Mon YYYY HH:MM:SS GMT``.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: base36_to_int(s)
|
|
|
|
|
2017-01-19 00:51:29 +08:00
|
|
|
Converts a base 36 string to an integer.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: int_to_base36(i)
|
|
|
|
|
2017-01-19 00:51:29 +08:00
|
|
|
Converts a positive integer to a base 36 string.
|
2014-04-26 22:00:15 +08:00
|
|
|
|
2013-06-22 04:59:33 +08:00
|
|
|
.. function:: urlsafe_base64_encode(s)
|
|
|
|
|
2018-10-02 07:18:04 +08:00
|
|
|
Encodes a bytestring to a base64 string for use in URLs, stripping any
|
|
|
|
trailing equal signs.
|
|
|
|
|
2013-06-22 04:59:33 +08:00
|
|
|
.. function:: urlsafe_base64_decode(s)
|
|
|
|
|
|
|
|
Decodes a base64 encoded string, adding back any trailing equal signs that
|
|
|
|
might have been stripped.
|
|
|
|
|
2013-04-03 00:59:43 +08:00
|
|
|
``django.utils.module_loading``
|
|
|
|
===============================
|
|
|
|
|
|
|
|
.. module:: django.utils.module_loading
|
|
|
|
:synopsis: Functions for working with Python modules.
|
|
|
|
|
|
|
|
Functions for working with Python modules.
|
|
|
|
|
2014-01-21 04:15:14 +08:00
|
|
|
.. function:: import_string(dotted_path)
|
2013-04-03 00:59:43 +08:00
|
|
|
|
|
|
|
Imports a dotted module path and returns the attribute/class designated by
|
2014-01-21 04:15:14 +08:00
|
|
|
the last name in the path. Raises ``ImportError`` if the import failed. For
|
|
|
|
example::
|
2013-04-03 00:59:43 +08:00
|
|
|
|
2014-01-21 04:15:14 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2014-05-13 19:04:49 +08:00
|
|
|
ValidationError = import_string('django.core.exceptions.ValidationError')
|
2013-04-03 00:59:43 +08:00
|
|
|
|
|
|
|
is equivalent to::
|
|
|
|
|
2014-05-13 19:04:49 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
2013-04-03 00:59:43 +08:00
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
``django.utils.safestring``
|
|
|
|
===========================
|
|
|
|
|
|
|
|
.. module:: django.utils.safestring
|
2014-03-03 21:49:18 +08:00
|
|
|
:synopsis: Functions and classes for working with strings that can be displayed safely without further escaping in HTML.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
Functions and classes for working with "safe strings": strings that can be
|
|
|
|
displayed safely without further escaping in HTML. Marking something as a "safe
|
|
|
|
string" means that the producer of the string has already turned characters
|
|
|
|
that should not be interpreted by the HTML engine (e.g. '<') into the
|
|
|
|
appropriate entities.
|
|
|
|
|
|
|
|
.. class:: SafeString
|
|
|
|
|
2019-02-05 22:38:29 +08:00
|
|
|
A ``str`` subclass that has been specifically marked as "safe" (requires no
|
|
|
|
further escaping) for HTML output purposes.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: mark_safe(s)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Explicitly mark a string as safe for (HTML) output purposes. The returned
|
2017-01-21 05:04:05 +08:00
|
|
|
object can be used everywhere a string is appropriate.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Can be called multiple times on a single string.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2016-06-03 05:11:43 +08:00
|
|
|
Can also be used as a decorator.
|
|
|
|
|
2015-01-15 16:24:54 +08:00
|
|
|
For building up fragments of HTML, you should normally be using
|
|
|
|
:func:`django.utils.html.format_html` instead.
|
|
|
|
|
2013-10-30 20:11:31 +08:00
|
|
|
String marked safe will become unsafe again if modified. For example::
|
|
|
|
|
|
|
|
>>> mystr = '<b>Hello World</b> '
|
|
|
|
>>> mystr = mark_safe(mystr)
|
|
|
|
>>> type(mystr)
|
2019-02-05 22:38:29 +08:00
|
|
|
<class 'django.utils.safestring.SafeString'>
|
2013-10-30 20:11:31 +08:00
|
|
|
|
|
|
|
>>> mystr = mystr.strip() # removing whitespace
|
|
|
|
>>> type(mystr)
|
|
|
|
<type 'str'>
|
|
|
|
|
2012-08-18 20:53:22 +08:00
|
|
|
``django.utils.text``
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. module:: django.utils.text
|
|
|
|
:synopsis: Text manipulation.
|
|
|
|
|
2016-08-25 00:18:17 +08:00
|
|
|
.. function:: format_lazy(format_string, *args, **kwargs)
|
|
|
|
|
|
|
|
A version of :meth:`str.format` for when ``format_string``, ``args``,
|
|
|
|
and/or ``kwargs`` contain lazy objects. The first argument is the string to
|
|
|
|
be formatted. For example::
|
|
|
|
|
|
|
|
from django.utils.text import format_lazy
|
|
|
|
from django.utils.translation import pgettext_lazy
|
|
|
|
|
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path(format_lazy('{person}/<int:pk>/', person=pgettext_lazy('URL', 'person')),
|
|
|
|
PersonDetailView.as_view()),
|
2016-08-25 00:18:17 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
This example allows translators to translate part of the URL. If "person"
|
|
|
|
is translated to "persona", the regular expression will match
|
|
|
|
``persona/(?P<pk>\d+)/$``, e.g. ``persona/5/``.
|
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
.. function:: slugify(value, allow_unicode=False)
|
2012-08-18 20:53:22 +08:00
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
Converts a string to a URL slug by:
|
2012-08-18 20:53:22 +08:00
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
#. Converting to ASCII if ``allow_unicode`` is ``False`` (the default).
|
|
|
|
#. Removing characters that aren't alphanumerics, underscores, hyphens, or
|
|
|
|
whitespace.
|
|
|
|
#. Removing leading and trailing whitespace.
|
|
|
|
#. Converting to lowercase.
|
|
|
|
#. Replacing any whitespace or repeated dashes with single dashes.
|
2012-08-18 20:53:22 +08:00
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
For example::
|
2012-08-18 20:53:22 +08:00
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
>>> slugify(' Joel is a slug ')
|
|
|
|
'joel-is-a-slug'
|
2015-04-16 06:28:49 +08:00
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
If you want to allow Unicode characters, pass ``allow_unicode=True``. For
|
|
|
|
example::
|
2015-04-16 06:28:49 +08:00
|
|
|
|
2018-09-15 04:55:17 +08:00
|
|
|
>>> slugify('你好 World', allow_unicode=True)
|
|
|
|
'你好-world'
|
2015-04-16 06:28:49 +08:00
|
|
|
|
2014-07-12 02:27:14 +08:00
|
|
|
.. _time-zone-selection-functions:
|
|
|
|
|
|
|
|
``django.utils.timezone``
|
|
|
|
=========================
|
|
|
|
|
|
|
|
.. module:: django.utils.timezone
|
|
|
|
:synopsis: Timezone support.
|
|
|
|
|
|
|
|
.. data:: utc
|
|
|
|
|
|
|
|
:class:`~datetime.tzinfo` instance that represents UTC.
|
|
|
|
|
|
|
|
.. function:: get_fixed_timezone(offset)
|
|
|
|
|
|
|
|
Returns a :class:`~datetime.tzinfo` instance that represents a time zone
|
|
|
|
with a fixed offset from UTC.
|
|
|
|
|
|
|
|
``offset`` is a :class:`datetime.timedelta` or an integer number of
|
|
|
|
minutes. Use positive values for time zones east of UTC and negative
|
|
|
|
values for west of UTC.
|
|
|
|
|
|
|
|
.. function:: get_default_timezone()
|
|
|
|
|
|
|
|
Returns a :class:`~datetime.tzinfo` instance that represents the
|
|
|
|
:ref:`default time zone <default-current-time-zone>`.
|
|
|
|
|
|
|
|
.. function:: get_default_timezone_name()
|
|
|
|
|
|
|
|
Returns the name of the :ref:`default time zone
|
|
|
|
<default-current-time-zone>`.
|
|
|
|
|
|
|
|
.. function:: get_current_timezone()
|
|
|
|
|
|
|
|
Returns a :class:`~datetime.tzinfo` instance that represents the
|
|
|
|
:ref:`current time zone <default-current-time-zone>`.
|
|
|
|
|
|
|
|
.. function:: get_current_timezone_name()
|
|
|
|
|
|
|
|
Returns the name of the :ref:`current time zone
|
|
|
|
<default-current-time-zone>`.
|
|
|
|
|
|
|
|
.. function:: activate(timezone)
|
|
|
|
|
|
|
|
Sets the :ref:`current time zone <default-current-time-zone>`. The
|
|
|
|
``timezone`` argument must be an instance of a :class:`~datetime.tzinfo`
|
2016-10-08 09:06:49 +08:00
|
|
|
subclass or a time zone name.
|
2014-07-12 02:27:14 +08:00
|
|
|
|
|
|
|
.. function:: deactivate()
|
|
|
|
|
|
|
|
Unsets the :ref:`current time zone <default-current-time-zone>`.
|
|
|
|
|
|
|
|
.. function:: override(timezone)
|
|
|
|
|
|
|
|
This is a Python context manager that sets the :ref:`current time zone
|
|
|
|
<default-current-time-zone>` on entry with :func:`activate()`, and restores
|
|
|
|
the previously active time zone on exit. If the ``timezone`` argument is
|
|
|
|
``None``, the :ref:`current time zone <default-current-time-zone>` is unset
|
|
|
|
on entry with :func:`deactivate()` instead.
|
|
|
|
|
2015-09-13 04:27:30 +08:00
|
|
|
``override`` is also usable as a function decorator.
|
2014-08-31 02:06:38 +08:00
|
|
|
|
2016-09-01 08:19:33 +08:00
|
|
|
.. function:: localtime(value=None, timezone=None)
|
2014-07-12 02:27:14 +08:00
|
|
|
|
|
|
|
Converts an aware :class:`~datetime.datetime` to a different time zone,
|
|
|
|
by default the :ref:`current time zone <default-current-time-zone>`.
|
|
|
|
|
2016-09-01 08:19:33 +08:00
|
|
|
When ``value`` is omitted, it defaults to :func:`now`.
|
|
|
|
|
2014-07-12 02:27:14 +08:00
|
|
|
This function doesn't work on naive datetimes; use :func:`make_aware`
|
|
|
|
instead.
|
|
|
|
|
2016-09-01 08:19:33 +08:00
|
|
|
.. function:: localdate(value=None, timezone=None)
|
|
|
|
|
|
|
|
Uses :func:`localtime` to convert an aware :class:`~datetime.datetime` to a
|
|
|
|
:meth:`~datetime.datetime.date` in a different time zone, by default the
|
|
|
|
:ref:`current time zone <default-current-time-zone>`.
|
|
|
|
|
|
|
|
When ``value`` is omitted, it defaults to :func:`now`.
|
|
|
|
|
|
|
|
This function doesn't work on naive datetimes.
|
|
|
|
|
2014-07-12 02:27:14 +08:00
|
|
|
.. function:: now()
|
|
|
|
|
|
|
|
Returns a :class:`~datetime.datetime` that represents the
|
|
|
|
current point in time. Exactly what's returned depends on the value of
|
|
|
|
:setting:`USE_TZ`:
|
|
|
|
|
|
|
|
* If :setting:`USE_TZ` is ``False``, this will be a
|
|
|
|
:ref:`naive <naive_vs_aware_datetimes>` datetime (i.e. a datetime
|
|
|
|
without an associated timezone) that represents the current time
|
|
|
|
in the system's local timezone.
|
|
|
|
|
|
|
|
* If :setting:`USE_TZ` is ``True``, this will be an
|
|
|
|
:ref:`aware <naive_vs_aware_datetimes>` datetime representing the
|
|
|
|
current time in UTC. Note that :func:`now` will always return
|
|
|
|
times in UTC regardless of the value of :setting:`TIME_ZONE`;
|
2017-12-20 23:44:14 +08:00
|
|
|
you can use :func:`localtime` to get the time in the current time zone.
|
2014-07-12 02:27:14 +08:00
|
|
|
|
|
|
|
.. function:: is_aware(value)
|
|
|
|
|
|
|
|
Returns ``True`` if ``value`` is aware, ``False`` if it is naive. This
|
|
|
|
function assumes that ``value`` is a :class:`~datetime.datetime`.
|
|
|
|
|
|
|
|
.. function:: is_naive(value)
|
|
|
|
|
|
|
|
Returns ``True`` if ``value`` is naive, ``False`` if it is aware. This
|
|
|
|
function assumes that ``value`` is a :class:`~datetime.datetime`.
|
|
|
|
|
2015-03-31 13:58:37 +08:00
|
|
|
.. function:: make_aware(value, timezone=None, is_dst=None)
|
2014-07-12 02:27:14 +08:00
|
|
|
|
|
|
|
Returns an aware :class:`~datetime.datetime` that represents the same
|
|
|
|
point in time as ``value`` in ``timezone``, ``value`` being a naive
|
2014-10-16 10:52:41 +08:00
|
|
|
:class:`~datetime.datetime`. If ``timezone`` is set to ``None``, it
|
|
|
|
defaults to the :ref:`current time zone <default-current-time-zone>`.
|
2014-07-12 02:27:14 +08:00
|
|
|
|
2016-10-08 09:06:49 +08:00
|
|
|
The ``pytz.AmbiguousTimeError`` exception is raised if you try to make
|
|
|
|
``value`` aware during a DST transition where the same time occurs twice
|
|
|
|
(when reverting from DST). Setting ``is_dst`` to ``True`` or ``False`` will
|
|
|
|
avoid the exception by choosing if the time is pre-transition or
|
|
|
|
post-transition respectively.
|
2015-03-31 13:58:37 +08:00
|
|
|
|
2016-10-08 09:06:49 +08:00
|
|
|
The ``pytz.NonExistentTimeError`` exception is raised if you try to make
|
2019-09-27 01:46:20 +08:00
|
|
|
``value`` aware during a DST transition such that the time never occurred.
|
|
|
|
For example, if the 2:00 hour is skipped during a DST transition, trying to
|
|
|
|
make 2:30 aware in that time zone will raise an exception. To avoid that
|
|
|
|
you can use ``is_dst`` to specify how ``make_aware()`` should interpret
|
|
|
|
such a nonexistent time. If ``is_dst=True`` then the above time would be
|
|
|
|
interpreted as 2:30 DST time (equivalent to 1:30 local time). Conversely,
|
|
|
|
if ``is_dst=False`` the time would be interpreted as 2:30 standard time
|
|
|
|
(equivalent to 3:30 local time).
|
2014-07-12 02:27:14 +08:00
|
|
|
|
2014-10-16 10:52:41 +08:00
|
|
|
.. function:: make_naive(value, timezone=None)
|
2014-07-12 02:27:14 +08:00
|
|
|
|
2017-10-31 09:47:09 +08:00
|
|
|
Returns a naive :class:`~datetime.datetime` that represents in
|
2014-07-12 02:27:14 +08:00
|
|
|
``timezone`` the same point in time as ``value``, ``value`` being an
|
2014-10-16 10:52:41 +08:00
|
|
|
aware :class:`~datetime.datetime`. If ``timezone`` is set to ``None``, it
|
|
|
|
defaults to the :ref:`current time zone <default-current-time-zone>`.
|
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
``django.utils.translation``
|
|
|
|
============================
|
|
|
|
|
|
|
|
.. module:: django.utils.translation
|
|
|
|
:synopsis: Internationalization support.
|
|
|
|
|
|
|
|
For a complete discussion on the usage of the following see the
|
2011-10-23 01:17:57 +08:00
|
|
|
:doc:`translation documentation </topics/i18n/translation>`.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2017-01-27 03:56:34 +08:00
|
|
|
The ``u`` prefix on the functions below comes from a difference in Python 2
|
|
|
|
between unicode and bytestrings. If your code doesn't support Python 2, use the
|
|
|
|
functions without the ``u``.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2017-01-27 03:56:34 +08:00
|
|
|
.. function:: gettext(message)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
Translates ``message`` and returns it as a string.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-06-15 18:48:13 +08:00
|
|
|
.. function:: pgettext(context, message)
|
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
Translates ``message`` given the ``context`` and returns it as a string.
|
2011-06-15 18:48:13 +08:00
|
|
|
|
|
|
|
For more information, see :ref:`contextual-markers`.
|
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
.. function:: gettext_lazy(message)
|
2011-06-15 18:48:13 +08:00
|
|
|
.. function:: pgettext_lazy(context, message)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Same as the non-lazy versions above, but using lazy execution.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
See :ref:`lazy translations documentation <lazy-translations>`.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: gettext_noop(message)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Marks strings for translation but doesn't translate them now. This can be
|
|
|
|
used to store strings in global variables that should stay in the base
|
|
|
|
language (because they might be used externally) and will be translated
|
|
|
|
later.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: ngettext(singular, plural, number)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Translates ``singular`` and ``plural`` and returns the appropriate string
|
2017-01-27 03:56:34 +08:00
|
|
|
based on ``number``.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-06-15 18:48:13 +08:00
|
|
|
.. function:: npgettext(context, singular, plural, number)
|
|
|
|
|
|
|
|
Translates ``singular`` and ``plural`` and returns the appropriate string
|
2017-01-27 03:56:34 +08:00
|
|
|
based on ``number`` and the ``context``.
|
2011-06-15 18:48:13 +08:00
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
.. function:: ngettext_lazy(singular, plural, number)
|
2013-11-09 06:20:50 +08:00
|
|
|
.. function:: npgettext_lazy(context, singular, plural, number)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Same as the non-lazy versions above, but using lazy execution.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
See :ref:`lazy translations documentation <lazy-translations>`.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: activate(language)
|
|
|
|
|
2014-03-02 23:00:30 +08:00
|
|
|
Fetches the translation object for a given language and activates it as
|
2011-08-25 17:20:44 +08:00
|
|
|
the current translation object for the current thread.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: deactivate()
|
|
|
|
|
2014-03-02 23:00:30 +08:00
|
|
|
Deactivates the currently active translation object so that further _ calls
|
2011-02-16 09:56:53 +08:00
|
|
|
will resolve against the default translation object, again.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: deactivate_all()
|
|
|
|
|
2014-03-02 23:00:30 +08:00
|
|
|
Makes the active translation object a ``NullTranslations()`` instance.
|
|
|
|
This is useful when we want delayed translations to appear as the original
|
|
|
|
string for some reason.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-09-08 21:24:41 +08:00
|
|
|
.. function:: override(language, deactivate=False)
|
2011-08-25 17:20:44 +08:00
|
|
|
|
|
|
|
A Python context manager that uses
|
|
|
|
:func:`django.utils.translation.activate` to fetch the translation object
|
2014-03-02 23:00:30 +08:00
|
|
|
for a given language, activates it as the translation object for the
|
|
|
|
current thread and reactivates the previous active language on exit.
|
2019-06-17 22:54:55 +08:00
|
|
|
Optionally, it can deactivate the temporary translation on exit with
|
2014-03-02 23:00:30 +08:00
|
|
|
:func:`django.utils.translation.deactivate` if the ``deactivate`` argument
|
|
|
|
is ``True``. If you pass ``None`` as the language argument, a
|
|
|
|
``NullTranslations()`` instance is activated within the context.
|
2011-08-25 17:20:44 +08:00
|
|
|
|
2015-09-13 04:27:30 +08:00
|
|
|
``override`` is also usable as a function decorator.
|
2014-08-30 05:23:13 +08:00
|
|
|
|
2015-10-07 16:46:08 +08:00
|
|
|
.. function:: check_for_language(lang_code)
|
|
|
|
|
|
|
|
Checks whether there is a global language file for the given language
|
|
|
|
code (e.g. 'fr', 'pt_BR'). This is used to decide whether a user-provided
|
|
|
|
language is available.
|
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
.. function:: get_language()
|
|
|
|
|
2015-01-08 18:11:35 +08:00
|
|
|
Returns the currently selected language code. Returns ``None`` if
|
|
|
|
translations are temporarily deactivated (by :func:`deactivate_all()` or
|
|
|
|
when ``None`` is passed to :func:`override()`).
|
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
.. function:: get_language_bidi()
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Returns selected language's BiDi layout:
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``False`` = left-to-right layout
|
|
|
|
* ``True`` = right-to-left layout
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2012-03-03 00:56:11 +08:00
|
|
|
.. function:: get_language_from_request(request, check_path=False)
|
|
|
|
|
2014-03-02 23:00:30 +08:00
|
|
|
Analyzes the request to find what language the user wants the system to
|
|
|
|
show. Only languages listed in settings.LANGUAGES are taken into account.
|
|
|
|
If the user requests a sublanguage where we have a main language, we send
|
|
|
|
out the main language.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2012-03-03 01:16:52 +08:00
|
|
|
If ``check_path`` is ``True``, the function first checks the requested URL
|
|
|
|
for whether its path begins with a language code listed in the
|
2012-03-03 00:56:11 +08:00
|
|
|
:setting:`LANGUAGES` setting.
|
|
|
|
|
2017-06-28 07:50:26 +08:00
|
|
|
.. function:: get_supported_language_variant(lang_code, strict=False)
|
|
|
|
|
|
|
|
Returns ``lang_code`` if it's in the :setting:`LANGUAGES` setting, possibly
|
|
|
|
selecting a more generic variant. For example, ``'es'`` is returned if
|
|
|
|
``lang_code`` is ``'es-ar'`` and ``'es'`` is in :setting:`LANGUAGES` but
|
|
|
|
``'es-ar'`` isn't.
|
|
|
|
|
|
|
|
If ``strict`` is ``False`` (the default), a country-specific variant may
|
|
|
|
be returned when neither the language code nor its generic variant is found.
|
|
|
|
For example, if only ``'es-co'`` is in :setting:`LANGUAGES`, that's
|
|
|
|
returned for ``lang_code``\s like ``'es'`` and ``'es-ar'``. Those matches
|
|
|
|
aren't returned if ``strict=True``.
|
|
|
|
|
|
|
|
Raises :exc:`LookupError` if nothing is found.
|
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
.. function:: to_locale(language)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Turns a language name (en-us) into a locale name (en_US).
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: templatize(src)
|
|
|
|
|
2014-03-02 23:00:30 +08:00
|
|
|
Turns a Django template into something that is understood by ``xgettext``.
|
|
|
|
It does so by translating the Django translation tags into standard
|
|
|
|
``gettext`` function invocations.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2014-02-22 21:27:57 +08:00
|
|
|
.. data:: LANGUAGE_SESSION_KEY
|
|
|
|
|
2014-03-02 23:00:30 +08:00
|
|
|
Session key under which the active language for the current session is
|
|
|
|
stored.
|
2016-08-15 04:42:49 +08:00
|
|
|
|
|
|
|
.. deprecated:: 3.0
|
|
|
|
|
|
|
|
The language won't be stored in the session in Django 4.0. Use the
|
|
|
|
:setting:`LANGUAGE_COOKIE_NAME` cookie instead.
|