2010-05-08 21:38:27 +00: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.
|
|
|
|
|
|
|
|
This module contains helper functions for controlling 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.
|
|
|
|
|
2011-09-04 21:17:30 +00:00
|
|
|
For information on the ``Vary`` header, see :rfc:`2616#section-14.44` section
|
|
|
|
14.44.
|
2010-05-08 21:38:27 +00: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.
|
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
For example, :doc:`internationalization </topics/i18n/index>` middleware would need
|
2010-05-08 21:38:27 +00:00
|
|
|
to distinguish caches by the ``Accept-language`` header.
|
|
|
|
|
|
|
|
.. function:: patch_cache_control(response, **kwargs)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
This function patches the ``Cache-Control`` header by adding all keyword
|
|
|
|
arguments to it. The transformation is as follows:
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-10-14 00:12:01 +00: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-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: get_max_age(response)
|
|
|
|
|
2011-02-16 01:56:53 +00: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-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: patch_response_headers(response, cache_timeout=None)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Adds some useful headers to the given ``HttpResponse`` object:
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* ``ETag``
|
|
|
|
* ``Last-Modified``
|
|
|
|
* ``Expires``
|
|
|
|
* ``Cache-Control``
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Each header is only added if it isn't already set.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-05-29 17:41:04 +00:00
|
|
|
``cache_timeout`` is in seconds. The :setting:`CACHE_MIDDLEWARE_SECONDS`
|
|
|
|
setting is used by default.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: add_never_cache_headers(response)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Adds headers to a response to indicate that a page should never be cached.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: patch_vary_headers(response, newheaders)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Adds (or updates) the ``Vary`` header in the given ``HttpResponse`` object.
|
|
|
|
``newheaders`` is a list of header names that should be in ``Vary``.
|
|
|
|
Existing headers in ``Vary`` aren't removed.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: get_cache_key(request, key_prefix=None)
|
|
|
|
|
2011-02-16 01:56:53 +00: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-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
If there is no headerlist stored, the page needs to be rebuilt, so this
|
|
|
|
function returns ``None``.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: learn_cache_key(request, response, cache_timeout=None, key_prefix=None)
|
|
|
|
|
2011-02-16 01:56:53 +00: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-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00: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
|
|
|
|
cache, this just 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-08 21:38:27 +00:00
|
|
|
|
2011-04-17 20:51:19 +00:00
|
|
|
``django.utils.datastructures``
|
|
|
|
===============================
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. module:: django.utils.datastructures
|
2011-04-17 20:51:19 +00:00
|
|
|
:synopsis: Data structures that aren't in Python's standard library.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
.. class:: SortedDict
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2013-08-03 05:41:15 +00:00
|
|
|
.. deprecated:: 1.7
|
|
|
|
``SortedDict`` is deprecated and will be removed in Django 1.9. Use
|
|
|
|
:class:`collections.OrderedDict` instead.
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
The :class:`django.utils.datastructures.SortedDict` class is a dictionary
|
|
|
|
that keeps its keys in the order in which they're inserted.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Creating a new SortedDict
|
|
|
|
-------------------------
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
Creating a new ``SortedDict`` must be done in a way where ordering is
|
|
|
|
guaranteed. For example::
|
|
|
|
|
|
|
|
SortedDict({'b': 1, 'a': 2, 'c': 3})
|
|
|
|
|
|
|
|
will not work. Passing in a basic Python ``dict`` could produce unreliable
|
|
|
|
results. Instead do::
|
|
|
|
|
|
|
|
SortedDict([('b', 1), ('a', 2), ('c', 3)])
|
|
|
|
|
2011-11-18 13:01:06 +00:00
|
|
|
``django.utils.dateparse``
|
|
|
|
==========================
|
|
|
|
|
|
|
|
.. module:: django.utils.dateparse
|
|
|
|
:synopsis: Functions to parse datetime objects.
|
|
|
|
|
|
|
|
The functions defined in this module share the following properties:
|
|
|
|
|
2012-12-25 14:56:22 +00:00
|
|
|
- They raise :exc:`~exceptions.ValueError` if their input is well formatted but
|
|
|
|
isn't a valid date or time.
|
2011-11-18 13:01:06 +00: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``.
|
|
|
|
|
|
|
|
.. function:: parse_datetime(value)
|
|
|
|
|
|
|
|
Parses a string and returns a :class:`datetime.datetime`.
|
|
|
|
|
|
|
|
UTC offsets are supported; if ``value`` describes one, the result's
|
|
|
|
``tzinfo`` attribute is a :class:`~django.utils.tzinfo.FixedOffset`
|
|
|
|
instance.
|
|
|
|
|
2012-09-30 17:37:25 +00:00
|
|
|
``django.utils.decorators``
|
|
|
|
===========================
|
|
|
|
|
|
|
|
.. module:: django.utils.decorators
|
|
|
|
:synopsis: Functions that help with creating decorators for views.
|
|
|
|
|
|
|
|
.. function:: method_decorator(decorator)
|
|
|
|
|
|
|
|
Converts a function decorator into a method decorator. See :ref:`decorating
|
|
|
|
class based views<decorating-class-based-views>` for example usage.
|
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
|
|
|
.. 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 13:12:42 +00:00
|
|
|
decorator is created from the ``CacheMiddleware`` like this::
|
2012-09-30 17:37:25 +00:00
|
|
|
|
|
|
|
cache_page = decorator_from_middleware_with_args(CacheMiddleware)
|
|
|
|
|
|
|
|
@cache_page(3600)
|
|
|
|
def my_view(request):
|
|
|
|
pass
|
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
``django.utils.encoding``
|
|
|
|
=========================
|
|
|
|
|
|
|
|
.. module:: django.utils.encoding
|
2013-06-29 09:52:29 +00:00
|
|
|
:synopsis: A series of helper functions to manage character encoding.
|
2012-07-21 08:00:10 +00:00
|
|
|
|
2012-08-11 13:34:51 +00:00
|
|
|
.. function:: python_2_unicode_compatible
|
|
|
|
|
|
|
|
A decorator that defines ``__unicode__`` and ``__str__`` methods under
|
|
|
|
Python 2. Under Python 3 it does nothing.
|
|
|
|
|
|
|
|
To support Python 2 and 3 with a single code base, define a ``__str__``
|
|
|
|
method returning text and apply this decorator to the class.
|
|
|
|
|
2012-07-21 08:00:10 +00:00
|
|
|
.. function:: smart_text(s, encoding='utf-8', strings_only=False, errors='strict')
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2012-07-21 08:00:10 +00:00
|
|
|
.. versionadded:: 1.5
|
|
|
|
|
|
|
|
Returns a text object representing ``s`` -- ``unicode`` on Python 2 and
|
|
|
|
``str`` on Python 3. Treats bytestrings using the ``encoding`` codec.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
If ``strings_only`` is ``True``, don't convert (some) non-string-like
|
|
|
|
objects.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2012-07-21 08:00:10 +00:00
|
|
|
.. function:: smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
|
|
|
Historical name of :func:`smart_text`. Only available under Python 2.
|
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
.. function:: is_protected_type(obj)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Determine if the object instance is of a protected type.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Objects of protected types are preserved as-is when passed to
|
2012-07-21 08:00:10 +00:00
|
|
|
``force_text(strings_only=True)``.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2012-07-21 08:00:10 +00:00
|
|
|
.. function:: force_text(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
|
|
|
.. versionadded:: 1.5
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2012-07-21 08:00:10 +00:00
|
|
|
Similar to ``smart_text``, except that lazy instances are resolved to
|
2011-02-16 01:56:53 +00:00
|
|
|
strings, rather than kept as lazy objects.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
If ``strings_only`` is ``True``, don't convert (some) non-string-like
|
|
|
|
objects.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2012-07-21 08:00:10 +00:00
|
|
|
.. function:: force_unicode(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
|
|
|
Historical name of :func:`force_text`. Only available under Python 2.
|
|
|
|
|
|
|
|
.. function:: smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
|
|
|
.. versionadded:: 1.5
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Returns a bytestring version of ``s``, encoded as specified in
|
|
|
|
``encoding``.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
If ``strings_only`` is ``True``, don't convert (some) non-string-like
|
|
|
|
objects.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2012-08-18 14:36:55 +00:00
|
|
|
.. function:: force_bytes(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
|
|
|
.. versionadded:: 1.5
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2012-07-21 08:00:10 +00:00
|
|
|
.. function:: smart_str(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
|
|
|
Alias of :func:`smart_bytes` on Python 2 and :func:`smart_text` on Python
|
2013-01-01 13:12:42 +00:00
|
|
|
3. This function returns a ``str`` or a lazy string.
|
2012-07-21 08:00:10 +00:00
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
For instance, this is suitable for writing to :data:`sys.stdout` on
|
2012-07-21 08:00:10 +00:00
|
|
|
Python 2 and 3.
|
|
|
|
|
2012-08-18 14:36:55 +00:00
|
|
|
.. function:: force_str(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
|
|
|
Alias of :func:`force_bytes` on Python 2 and :func:`force_text` on Python
|
2013-01-01 13:12:42 +00:00
|
|
|
3. This function always returns a ``str``.
|
2012-08-18 14:36:55 +00:00
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
.. function:: iri_to_uri(iri)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Convert an Internationalized Resource Identifier (IRI) portion to a URI
|
|
|
|
portion that is suitable for inclusion in a URL.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-09-04 21:17:30 +00:00
|
|
|
This is the algorithm from section 3.1 of :rfc:`3987#section-3.1`. However,
|
|
|
|
since we are assuming input is either UTF-8 or unicode already, we can
|
|
|
|
simplify things a little from the full method.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Returns an ASCII string containing the encoded result.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2012-12-16 19:34:41 +00:00
|
|
|
.. function:: filepath_to_uri(path)
|
|
|
|
|
|
|
|
Convert a file system path to a URI portion that is suitable for inclusion
|
|
|
|
in a URL. The path is assumed to be either UTF-8 or unicode.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
``django.utils.feedgenerator``
|
|
|
|
==============================
|
|
|
|
|
|
|
|
.. module:: django.utils.feedgenerator
|
|
|
|
:synopsis: Syndication feed generation library -- used for generating RSS, etc.
|
|
|
|
|
|
|
|
Sample usage::
|
|
|
|
|
|
|
|
>>> from django.utils import feedgenerator
|
|
|
|
>>> feed = feedgenerator.Rss201rev2Feed(
|
|
|
|
... title=u"Poynter E-Media Tidbits",
|
|
|
|
... link=u"http://www.poynter.org/column.asp?id=31",
|
2010-10-09 08:12:50 +00:00
|
|
|
... description=u"A group Weblog by the sharpest minds in online media/journalism/publishing.",
|
2010-05-08 21:38:27 +00:00
|
|
|
... language=u"en",
|
|
|
|
... )
|
|
|
|
>>> feed.add_item(
|
|
|
|
... title="Hello",
|
|
|
|
... link=u"http://www.holovaty.com/test/",
|
|
|
|
... description="Testing."
|
|
|
|
... )
|
2012-05-05 12:01:38 +00:00
|
|
|
>>> with open('test.rss', 'w') as fp:
|
2012-05-05 14:54:30 +00:00
|
|
|
... feed.write(fp, 'utf-8')
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
For simplifying the selection of a generator use ``feedgenerator.DefaultFeed``
|
|
|
|
which is currently ``Rss201rev2Feed``
|
|
|
|
|
|
|
|
For definitions of the different versions of RSS, see:
|
2012-06-28 08:49:07 +00:00
|
|
|
http://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/02/04/incompatible-rss
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: get_tag_uri(url, date)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Creates a TagURI.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2012-06-28 08:49:07 +00:00
|
|
|
See http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
SyndicationFeed
|
|
|
|
---------------
|
|
|
|
|
|
|
|
.. class:: SyndicationFeed
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Base class for all syndication feeds. Subclasses should provide write().
|
2011-05-14 12:28:36 +00:00
|
|
|
|
2011-03-03 22:20:54 +00: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])
|
|
|
|
|
|
|
|
Initialize the feed with the given dictionary of metadata, which applies
|
|
|
|
to the entire feed.
|
2011-05-14 12:28:36 +00:00
|
|
|
|
2011-03-03 22:20:54 +00:00
|
|
|
Any extra keyword arguments you pass to ``__init__`` will be stored in
|
|
|
|
``self.feed``.
|
|
|
|
|
|
|
|
All parameters should be Unicode objects, except ``categories``, which
|
|
|
|
should be a sequence of Unicode objects.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2013-07-17 19:20:20 +00:00
|
|
|
.. method:: add_item(title, link, description, [author_email=None, author_name=None, author_link=None, pubdate=None, comments=None, unique_id=None, enclosure=None, categories=(), item_copyright=None, ttl=None, updateddate=None, **kwargs])
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Adds an item to the feed. All args are expected to be Python ``unicode``
|
2013-07-17 19:20:20 +00:00
|
|
|
objects except ``pubdate`` and ``updateddate``, which are ``datetime.datetime``
|
|
|
|
objects, and ``enclosure``, which is an instance of the ``Enclosure`` class.
|
|
|
|
|
|
|
|
.. versionadded:: 1.7
|
|
|
|
|
|
|
|
The optional ``updateddate`` argument was added.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
.. method:: num_items()
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
.. method:: root_attributes()
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Return extra attributes to place on the root (i.e. feed/channel)
|
2011-03-03 22:20:54 +00:00
|
|
|
element. Called from ``write()``.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
.. method:: add_root_elements(handler)
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Add elements in the root (i.e. feed/channel) element.
|
2011-03-03 22:20:54 +00:00
|
|
|
Called from ``write()``.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
.. method:: item_attributes(item)
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Return extra attributes to place on each item (i.e. item/entry)
|
|
|
|
element.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
.. method:: add_item_elements(handler, item)
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Add elements on each item (i.e. item/entry) element.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
.. method:: write(outfile, encoding)
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Outputs the feed in the given encoding to ``outfile``, which is a
|
|
|
|
file-like object. Subclasses should override this.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
.. method:: writeString(encoding)
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Returns the feed in the given encoding as a string.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
.. method:: latest_post_date()
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2013-07-17 19:20:20 +00:00
|
|
|
Returns the latest ``pubdate`` or ``updateddate`` for all items in the
|
|
|
|
feed. If no items have either of these attributes this returns the
|
|
|
|
current date/time.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
Enclosure
|
|
|
|
---------
|
|
|
|
|
|
|
|
.. class:: Enclosure
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Represents an RSS enclosure
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
RssFeed
|
|
|
|
-------
|
|
|
|
|
|
|
|
.. class:: RssFeed(SyndicationFeed)
|
|
|
|
|
|
|
|
Rss201rev2Feed
|
|
|
|
--------------
|
|
|
|
|
|
|
|
.. class:: Rss201rev2Feed(RssFeed)
|
|
|
|
|
2012-06-28 08:49:07 +00:00
|
|
|
Spec: http://cyber.law.harvard.edu/rss/rss.html
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-03-03 22:20:54 +00:00
|
|
|
RssUserland091Feed
|
|
|
|
------------------
|
|
|
|
|
|
|
|
.. class:: RssUserland091Feed(RssFeed)
|
|
|
|
|
|
|
|
Spec: http://backend.userland.com/rss091
|
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
Atom1Feed
|
|
|
|
---------
|
|
|
|
|
|
|
|
.. class:: Atom1Feed(SyndicationFeed)
|
|
|
|
|
2013-07-18 11:53:54 +00:00
|
|
|
Spec: http://tools.ietf.org/html/rfc4287
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-06-15 10:48:13 +00:00
|
|
|
``django.utils.functional``
|
|
|
|
===========================
|
|
|
|
|
|
|
|
.. module:: django.utils.functional
|
|
|
|
:synopsis: Functional programming tools.
|
|
|
|
|
2013-08-07 17:47:07 +00:00
|
|
|
.. class:: cached_property(object)
|
|
|
|
|
|
|
|
The ``@cached_property`` decorator caches the result of a method with a
|
2013-08-08 12:16:48 +00: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-07 17:47:07 +00: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
|
|
|
|
context, where the template might invoke the method once more::
|
|
|
|
|
|
|
|
# the model
|
|
|
|
class Person(models.Model):
|
|
|
|
|
|
|
|
def friends(self):
|
|
|
|
# expensive computation
|
|
|
|
...
|
|
|
|
return friends
|
|
|
|
|
|
|
|
# in the view:
|
|
|
|
if person.friends():
|
|
|
|
|
|
|
|
# in the template:
|
|
|
|
{% for friend in person.friends %}
|
|
|
|
|
2013-08-08 12:16:48 +00:00
|
|
|
Here, ``friends()`` will be called twice. Since the instance ``person`` in
|
2013-08-07 17:47:07 +00:00
|
|
|
the view and the template are the same, ``@cached_property`` can avoid
|
|
|
|
that::
|
|
|
|
|
|
|
|
from django.utils.functional import cached_property
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def friends(self):
|
|
|
|
# expensive computation
|
|
|
|
...
|
|
|
|
return friends
|
|
|
|
|
|
|
|
Note that as the method is now a property, in Python code it will need to
|
|
|
|
be invoked appropriately::
|
|
|
|
|
|
|
|
# in the view:
|
|
|
|
if person.friends:
|
|
|
|
|
2013-08-08 12:16:48 +00: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"]
|
|
|
|
|
|
|
|
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
|
|
|
|
based on ``datetime.now()``, or simply 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-07 17:47:07 +00:00
|
|
|
|
2011-06-15 10:48:13 +00:00
|
|
|
.. function:: allow_lazy(func, *resultclasses)
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
If you write your own similar functions and deal with translations, you'll
|
|
|
|
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).
|
|
|
|
|
|
|
|
For cases like this, use the ``django.utils.functional.allow_lazy()``
|
|
|
|
decorator. It modifies the function so that *if* it's called with a lazy
|
|
|
|
translation as the first argument, the function evaluation is delayed until it
|
|
|
|
needs to be converted to a string.
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
from django.utils.functional import allow_lazy
|
|
|
|
|
|
|
|
def fancy_utility_function(s, ...):
|
|
|
|
# Do some conversion on string 's'
|
|
|
|
...
|
2013-07-04 13:19:33 +00:00
|
|
|
# Replace unicode by str on Python 3
|
2011-06-15 10:48:13 +00:00
|
|
|
fancy_utility_function = allow_lazy(fancy_utility_function, unicode)
|
|
|
|
|
|
|
|
The ``allow_lazy()`` decorator takes, in addition to the function to decorate,
|
|
|
|
a number of extra arguments (``*args``) specifying the type(s) that the
|
2013-07-04 13:19:33 +00:00
|
|
|
original function can return. Usually, it's enough to include ``unicode``
|
|
|
|
(or ``str`` on Python 3) here and ensure that your function returns only
|
|
|
|
Unicode strings.
|
2011-06-15 10:48:13 +00:00
|
|
|
|
|
|
|
Using this decorator means you can write your function and assume that the
|
|
|
|
input is a proper string, then add support for lazy translation objects at the
|
|
|
|
end.
|
|
|
|
|
2012-06-30 15:41:51 +00: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 12:00:17 +00:00
|
|
|
where appropriate. This module provides some additional low level utilities for
|
2012-06-30 15:41:51 +00:00
|
|
|
escaping HTML.
|
|
|
|
|
|
|
|
.. function:: escape(text)
|
|
|
|
|
|
|
|
Returns the given text with ampersands, quotes and angle brackets encoded
|
|
|
|
for use in HTML. The input is first passed through
|
2012-07-21 08:00:10 +00:00
|
|
|
:func:`~django.utils.encoding.force_text` and the output has
|
2012-06-30 15:41:51 +00:00
|
|
|
:func:`~django.utils.safestring.mark_safe` applied.
|
|
|
|
|
|
|
|
.. function:: conditional_escape(text)
|
|
|
|
|
|
|
|
Similar to ``escape()``, except that it doesn't operate on pre-escaped strings,
|
|
|
|
so it will not double escape.
|
2011-06-15 10:48:13 +00:00
|
|
|
|
2012-06-30 17:54:38 +00:00
|
|
|
.. function:: format_html(format_string, *args, **kwargs)
|
|
|
|
|
|
|
|
This is similar to `str.format`_, except that it is appropriate for
|
|
|
|
building up HTML fragments. All args and kwargs are passed through
|
|
|
|
:func:`conditional_escape` before being passed to ``str.format``.
|
|
|
|
|
|
|
|
For the case of building up small HTML fragments, this function is to be
|
|
|
|
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.
|
|
|
|
|
|
|
|
So, instead of writing:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
mark_safe(u"%s <b>%s</b> %s" % (some_html,
|
|
|
|
escape(some_text),
|
|
|
|
escape(some_other_text),
|
|
|
|
))
|
|
|
|
|
|
|
|
you should instead use:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2013-01-24 10:58:06 +00:00
|
|
|
format_html(u"{0} <b>{1}</b> {2}",
|
2012-06-30 17:54:38 +00:00
|
|
|
mark_safe(some_html), some_text, some_other_text)
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Note that although this function uses ``str.format`` to do the
|
|
|
|
interpolation, some of the formatting options provided by `str.format`_
|
|
|
|
(e.g. number formatting) will not work, since all arguments are passed
|
|
|
|
through :func:`conditional_escape` which (ultimately) calls
|
2012-07-21 08:00:10 +00:00
|
|
|
:func:`~django.utils.encoding.force_text` on the values.
|
2012-06-30 17:54:38 +00:00
|
|
|
|
2013-01-25 11:53:40 +00: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::
|
|
|
|
|
|
|
|
format_html_join('\n', "<li>{0} {1}</li>", ((u.first_name, u.last_name)
|
|
|
|
for u in users))
|
|
|
|
|
2012-08-18 12:53:22 +00:00
|
|
|
.. function:: strip_tags(value)
|
|
|
|
|
|
|
|
Removes anything that looks like an html tag from the string, that is
|
|
|
|
anything contained within ``<>``.
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
strip_tags(value)
|
|
|
|
|
|
|
|
If ``value`` is ``"<b>Joel</b> <button>is</button> a <span>slug</span>"`` the
|
2013-05-23 12:00:17 +00:00
|
|
|
return value will be ``"Joel is a slug"``. Note that ``strip_tags`` result
|
|
|
|
may still contain unsafe HTML content, so you might use
|
|
|
|
:func:`~django.utils.html.escape` to make it a safe string.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.6
|
|
|
|
|
|
|
|
For improved safety, ``strip_tags`` is now parser-based.
|
2012-08-18 12:53:22 +00:00
|
|
|
|
|
|
|
.. function:: remove_tags(value, tags)
|
|
|
|
|
2013-03-28 01:51:44 +00:00
|
|
|
Removes a space-separated list of [X]HTML tag names from the output.
|
2012-08-18 12:53:22 +00:00
|
|
|
|
|
|
|
For example::
|
|
|
|
|
2013-03-28 01:51:44 +00:00
|
|
|
remove_tags(value, "b span")
|
2012-08-18 12:53:22 +00:00
|
|
|
|
|
|
|
If ``value`` is ``"<b>Joel</b> <button>is</button> a <span>slug</span>"`` the
|
|
|
|
return value will be ``"Joel <button>is</button> a slug"``.
|
|
|
|
|
|
|
|
Note that this filter is case-sensitive.
|
|
|
|
|
|
|
|
If ``value`` is ``"<B>Joel</B> <button>is</button> a <span>slug</span>"`` the
|
|
|
|
return value will be ``"<B>Joel</B> <button>is</button> a slug"``.
|
2012-06-30 17:54:38 +00:00
|
|
|
|
|
|
|
.. _str.format: http://docs.python.org/library/stdtypes.html#str.format
|
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
``django.utils.http``
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. module:: django.utils.http
|
|
|
|
:synopsis: HTTP helper functions. (URL encoding, cookie handling, ...)
|
|
|
|
|
|
|
|
.. function:: urlquote(url, safe='/')
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
A version of Python's ``urllib.quote()`` function that can operate on
|
|
|
|
unicode strings. The url is first UTF-8 encoded before quoting. The
|
|
|
|
returned string can safely be used as part of an argument to a subsequent
|
|
|
|
``iri_to_uri()`` call without double-quoting occurring. Employs lazy
|
|
|
|
execution.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: urlquote_plus(url, safe='')
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
A version of Python's urllib.quote_plus() function that can operate on
|
|
|
|
unicode strings. The url is first UTF-8 encoded before quoting. The
|
|
|
|
returned string can safely be used as part of an argument to a subsequent
|
|
|
|
``iri_to_uri()`` call without double-quoting occurring. Employs lazy
|
|
|
|
execution.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: urlencode(query, doseq=0)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
A version of Python's urllib.urlencode() function that can operate on
|
|
|
|
unicode strings. The parameters are first case to UTF-8 encoded strings
|
|
|
|
and then encoded as per normal.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: cookie_date(epoch_seconds=None)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Formats the time to ensure compatibility with Netscape's cookie standard.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00: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-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Outputs a string in the format ``Wdy, DD-Mon-YYYY HH:MM:SS GMT``.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: http_date(epoch_seconds=None)
|
|
|
|
|
2011-09-04 21:17:30 +00:00
|
|
|
Formats the time to match the :rfc:`1123` date format as specified by HTTP
|
|
|
|
:rfc:`2616#section-3.3.1` section 3.3.1.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00: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-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Outputs a string in the format ``Wdy, DD Mon YYYY HH:MM:SS GMT``.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: base36_to_int(s)
|
|
|
|
|
2012-08-03 16:46:30 +00:00
|
|
|
Converts a base 36 string to an integer. On Python 2 the output is
|
2013-01-01 13:12:42 +00:00
|
|
|
guaranteed to be an ``int`` and not a ``long``.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: int_to_base36(i)
|
|
|
|
|
2012-08-03 16:46:30 +00:00
|
|
|
Converts a positive integer to a base 36 string. On Python 2 ``i`` must be
|
2013-01-01 13:12:42 +00:00
|
|
|
smaller than :data:`sys.maxint`.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2013-06-21 20:59:33 +00:00
|
|
|
.. function:: urlsafe_base64_encode(s)
|
|
|
|
|
|
|
|
.. versionadded:: 1.6
|
|
|
|
|
|
|
|
Encodes a bytestring in base64 for use in URLs, stripping any trailing
|
|
|
|
equal signs.
|
|
|
|
|
|
|
|
.. function:: urlsafe_base64_decode(s)
|
|
|
|
|
|
|
|
.. versionadded:: 1.6
|
|
|
|
|
|
|
|
Decodes a base64 encoded string, adding back any trailing equal signs that
|
|
|
|
might have been stripped.
|
|
|
|
|
2013-04-02 16:59:43 +00:00
|
|
|
``django.utils.module_loading``
|
|
|
|
===============================
|
|
|
|
|
|
|
|
.. module:: django.utils.module_loading
|
|
|
|
:synopsis: Functions for working with Python modules.
|
|
|
|
|
|
|
|
Functions for working with Python modules.
|
|
|
|
|
|
|
|
.. function:: import_by_path(dotted_path, error_prefix='')
|
|
|
|
|
2013-06-12 08:50:37 +00:00
|
|
|
.. versionadded:: 1.6
|
|
|
|
|
2013-04-02 16:59:43 +00:00
|
|
|
Imports a dotted module path and returns the attribute/class designated by
|
|
|
|
the last name in the path. Raises
|
|
|
|
:exc:`~django.core.exceptions.ImproperlyConfigured` if something goes
|
|
|
|
wrong. For example::
|
|
|
|
|
|
|
|
from django.utils.module_loading import import_by_path
|
2013-05-16 08:06:42 +00:00
|
|
|
ImproperlyConfigured = import_by_path('django.core.exceptions.ImproperlyConfigured')
|
2013-04-02 16:59:43 +00:00
|
|
|
|
|
|
|
is equivalent to::
|
|
|
|
|
2013-05-16 08:06:42 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2013-04-02 16:59:43 +00:00
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
``django.utils.safestring``
|
|
|
|
===========================
|
|
|
|
|
|
|
|
.. module:: django.utils.safestring
|
|
|
|
:synopsis: Functions and classes for working with strings that can be displayed safely without further escaping in HTML.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2012-08-18 14:04:06 +00:00
|
|
|
.. class:: SafeBytes
|
|
|
|
|
|
|
|
.. versionadded:: 1.5
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
A ``bytes`` subclass that has been specifically marked as "safe"
|
2012-08-18 14:04:06 +00:00
|
|
|
(requires no further escaping) for HTML output purposes.
|
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
.. class:: SafeString
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
A ``str`` subclass that has been specifically marked as "safe"
|
2012-08-18 14:04:06 +00:00
|
|
|
(requires no further escaping) for HTML output purposes. This is
|
|
|
|
:class:`SafeBytes` on Python 2 and :class:`SafeText` on Python 3.
|
|
|
|
|
|
|
|
.. class:: SafeText
|
|
|
|
|
|
|
|
.. versionadded:: 1.5
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
A ``str`` (in Python 3) or ``unicode`` (in Python 2) subclass
|
2012-08-18 14:04:06 +00:00
|
|
|
that has been specifically marked as "safe" for HTML output purposes.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. class:: SafeUnicode
|
|
|
|
|
2012-08-18 14:04:06 +00:00
|
|
|
Historical name of :class:`SafeText`. Only available under Python 2.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: mark_safe(s)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Explicitly mark a string as safe for (HTML) output purposes. The returned
|
|
|
|
object can be used everywhere a string or unicode object is appropriate.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Can be called multiple times on a single string.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: mark_for_escaping(s)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Explicitly mark a string as requiring HTML escaping upon output. Has no
|
|
|
|
effect on ``SafeData`` subclasses.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Can be called multiple times on a single string (the resulting escaping is
|
|
|
|
only applied once).
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2012-08-18 12:53:22 +00:00
|
|
|
``django.utils.text``
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. module:: django.utils.text
|
|
|
|
:synopsis: Text manipulation.
|
|
|
|
|
|
|
|
.. function:: slugify
|
|
|
|
|
|
|
|
Converts to lowercase, removes non-word characters (alphanumerics and
|
|
|
|
underscores) and converts spaces to hyphens. Also strips leading and trailing
|
|
|
|
whitespace.
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
slugify(value)
|
|
|
|
|
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``"joel-is-a-slug"``.
|
|
|
|
|
2010-05-08 21:38:27 +00: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-22 17:17:57 +00:00
|
|
|
:doc:`translation documentation </topics/i18n/translation>`.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: gettext(message)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Translates ``message`` and returns it in a UTF-8 bytestring
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: ugettext(message)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Translates ``message`` and returns it in a unicode string
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-06-15 10:48:13 +00:00
|
|
|
.. function:: pgettext(context, message)
|
|
|
|
|
|
|
|
Translates ``message`` given the ``context`` and returns
|
|
|
|
it in a unicode string.
|
|
|
|
|
|
|
|
For more information, see :ref:`contextual-markers`.
|
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
.. function:: gettext_lazy(message)
|
|
|
|
.. function:: ugettext_lazy(message)
|
2011-06-15 10:48:13 +00:00
|
|
|
.. function:: pgettext_lazy(context, message)
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Same as the non-lazy versions above, but using lazy execution.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
See :ref:`lazy translations documentation <lazy-translations>`.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: gettext_noop(message)
|
2011-06-15 10:48:13 +00:00
|
|
|
.. function:: ugettext_noop(message)
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00: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-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: ngettext(singular, plural, number)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Translates ``singular`` and ``plural`` and returns the appropriate string
|
|
|
|
based on ``number`` in a UTF-8 bytestring.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: ungettext(singular, plural, number)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Translates ``singular`` and ``plural`` and returns the appropriate string
|
|
|
|
based on ``number`` in a unicode string.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-06-15 10:48:13 +00:00
|
|
|
.. function:: npgettext(context, singular, plural, number)
|
|
|
|
|
|
|
|
Translates ``singular`` and ``plural`` and returns the appropriate string
|
|
|
|
based on ``number`` and the ``context`` in a unicode string.
|
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
.. function:: ngettext_lazy(singular, plural, number)
|
|
|
|
.. function:: ungettext_lazy(singular, plural, number)
|
2011-06-15 10:48:13 +00:00
|
|
|
.. function:: npgettext_lazy(singular, plural, number)
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Same as the non-lazy versions above, but using lazy execution.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
See :ref:`lazy translations documentation <lazy-translations>`.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: string_concat(*strings)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Lazy variant of string concatenation, needed for translations that are
|
|
|
|
constructed from multiple parts.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: activate(language)
|
|
|
|
|
2011-08-25 09:20:44 +00:00
|
|
|
Fetches the translation object for a given language and installs it as
|
|
|
|
the current translation object for the current thread.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: deactivate()
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
De-installs the currently active translation object so that further _ calls
|
|
|
|
will resolve against the default translation object, again.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: deactivate_all()
|
|
|
|
|
2011-02-16 01:56:53 +00: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-08 21:38:27 +00:00
|
|
|
|
2011-09-08 13:24:41 +00:00
|
|
|
.. function:: override(language, deactivate=False)
|
2011-08-25 09:20:44 +00:00
|
|
|
|
|
|
|
A Python context manager that uses
|
|
|
|
:func:`django.utils.translation.activate` to fetch the translation object
|
|
|
|
for a given language, installing it as the translation object for the
|
2011-09-08 13:24:41 +00:00
|
|
|
current thread and reinstall the previous active language on exit.
|
|
|
|
Optionally it can simply deinstall the temporary translation on exit with
|
|
|
|
:func:`django.utils.translation.deactivate` if the deactivate argument is
|
|
|
|
True. If you pass None as the language argument, a NullTranslations()
|
|
|
|
instance is installed while the context is active.
|
2011-08-25 09:20:44 +00:00
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
.. function:: get_language()
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Returns the currently selected language code.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: get_language_bidi()
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Returns selected language's BiDi layout:
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* ``False`` = left-to-right layout
|
|
|
|
* ``True`` = right-to-left layout
|
2010-05-08 21:38:27 +00:00
|
|
|
|
2012-03-02 16:56:11 +00:00
|
|
|
.. function:: get_language_from_request(request, check_path=False)
|
|
|
|
|
2011-02-16 01:56:53 +00: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-08 21:38:27 +00:00
|
|
|
|
2012-03-02 17:16:52 +00: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-02 16:56:11 +00:00
|
|
|
:setting:`LANGUAGES` setting.
|
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
.. function:: to_locale(language)
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Turns a language name (en-us) into a locale name (en_US).
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. function:: templatize(src)
|
|
|
|
|
2011-02-16 01:56:53 +00: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-08 21:38:27 +00:00
|
|
|
|
2011-11-18 13:01:06 +00: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_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`
|
|
|
|
subclass or, if pytz_ is available, a time zone name.
|
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
2012-04-29 13:37:23 +00:00
|
|
|
.. versionadded:: 1.5
|
|
|
|
|
|
|
|
.. function:: localtime(value, timezone=None)
|
|
|
|
|
|
|
|
Converts an aware :class:`~datetime.datetime` to a different time zone,
|
|
|
|
by default the :ref:`current time zone <default-current-time-zone>`.
|
|
|
|
|
|
|
|
This function doesn't work on naive datetimes; use :func:`make_aware`
|
|
|
|
instead.
|
|
|
|
|
2011-11-18 13:01:06 +00:00
|
|
|
.. function:: now()
|
|
|
|
|
2013-06-06 16:03:47 +00:00
|
|
|
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 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`;
|
|
|
|
you can use :func:`localtime` to convert to a time in the current
|
|
|
|
time zone.
|
2011-11-18 13:01:06 +00: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`.
|
|
|
|
|
|
|
|
.. function:: make_aware(value, timezone)
|
|
|
|
|
|
|
|
Returns an aware :class:`~datetime.datetime` that represents the same
|
|
|
|
point in time as ``value`` in ``timezone``, ``value`` being a naive
|
|
|
|
:class:`~datetime.datetime`.
|
|
|
|
|
|
|
|
This function can raise an exception if ``value`` doesn't exist or is
|
|
|
|
ambiguous because of DST transitions.
|
|
|
|
|
|
|
|
.. function:: make_naive(value, timezone)
|
|
|
|
|
|
|
|
Returns an naive :class:`~datetime.datetime` that represents in
|
|
|
|
``timezone`` the same point in time as ``value``, ``value`` being an
|
|
|
|
aware :class:`~datetime.datetime`
|
|
|
|
|
|
|
|
.. _pytz: http://pytz.sourceforge.net/
|
|
|
|
|
2010-05-08 21:38:27 +00:00
|
|
|
``django.utils.tzinfo``
|
|
|
|
=======================
|
|
|
|
|
|
|
|
.. module:: django.utils.tzinfo
|
|
|
|
:synopsis: Implementation of ``tzinfo`` classes for use with ``datetime.datetime``.
|
|
|
|
|
|
|
|
.. class:: FixedOffset
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Fixed offset in minutes east from UTC.
|
2010-05-08 21:38:27 +00:00
|
|
|
|
|
|
|
.. class:: LocalTimezone
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
Proxy timezone information from time module.
|