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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
For information on the ``Vary`` header, see `RFC 2616 section 14.44`_.
|
|
|
|
|
|
|
|
.. _RFC 2616 section 14.44: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.44
|
|
|
|
|
|
|
|
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-20 03:27:44 +08:00
|
|
|
For example, :doc:`internationalization </topics/i18n/index>` middleware would need
|
2010-05-09 05:38:27 +08:00
|
|
|
to distinguish caches by the ``Accept-language`` header.
|
|
|
|
|
|
|
|
.. 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-02-16 09:56:53 +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
|
|
|
|
|
|
|
.. 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-02-16 09:56:53 +08:00
|
|
|
* ``ETag``
|
|
|
|
* ``Last-Modified``
|
|
|
|
* ``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-02-16 09:56:53 +08:00
|
|
|
``cache_timeout`` is in seconds. The ``CACHE_MIDDLEWARE_SECONDS`` setting
|
|
|
|
is used by default.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: add_never_cache_headers(response)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Adds headers to a response to indicate that a page should never be cached.
|
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.
|
|
|
|
``newheaders`` is a list of header names that should be in ``Vary``.
|
|
|
|
Existing headers in ``Vary`` aren't removed.
|
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
|
|
|
|
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-09 05:38:27 +08:00
|
|
|
|
2011-04-18 04:51:19 +08:00
|
|
|
``django.utils.datastructures``
|
|
|
|
===============================
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. module:: django.utils.datastructures
|
2011-04-18 04:51:19 +08:00
|
|
|
:synopsis: Data structures that aren't in Python's standard library.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. class:: SortedDict
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
The :class:`django.utils.datastructures.SortedDict` class is a dictionary
|
|
|
|
that keeps its keys in the order in which they're inserted.
|
|
|
|
``SortedDict`` adds two additional methods to the standard Python ``dict``
|
|
|
|
class:
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: insert(index, key, value)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Inserts the key, value pair before the item with the given index.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. method:: value_for_index(index)
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Returns the value of the item at the given zero-based index.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Creating a new SortedDict
|
|
|
|
-------------------------
|
2010-05-09 05:38:27 +08: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)])
|
|
|
|
|
|
|
|
``django.utils.encoding``
|
|
|
|
=========================
|
|
|
|
|
|
|
|
.. module:: django.utils.encoding
|
|
|
|
:synopsis: A series of helper classes and function to manage character encoding.
|
|
|
|
|
|
|
|
.. class:: StrAndUnicode
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
A class whose ``__str__`` returns its ``__unicode__`` as a UTF-8
|
|
|
|
bytestring. Useful as a mix-in.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Returns a ``unicode`` object representing ``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
|
|
|
|
``force_unicode(strings_only=True)``.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: force_unicode(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Similar to ``smart_unicode``, except that lazy instances are resolved to
|
|
|
|
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
|
|
|
|
|
|
|
.. function:: smart_str(s, encoding='utf-8', strings_only=False, errors='strict')
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Returns a bytestring version of ``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
|
|
|
|
|
|
|
.. 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
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
This is the algorithm from section 3.1 of `RFC 3987`_. 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-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. _RFC 3987: http://www.ietf.org/rfc/rfc3987.txt
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Returns an ASCII string containing the encoded result.
|
2010-05-09 05:38:27 +08: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 16:12:50 +08:00
|
|
|
... description=u"A group Weblog by the sharpest minds in online media/journalism/publishing.",
|
2010-05-09 05:38:27 +08:00
|
|
|
... language=u"en",
|
|
|
|
... )
|
|
|
|
>>> feed.add_item(
|
|
|
|
... title="Hello",
|
|
|
|
... link=u"http://www.holovaty.com/test/",
|
|
|
|
... description="Testing."
|
|
|
|
... )
|
|
|
|
>>> fp = open('test.rss', 'w')
|
|
|
|
>>> feed.write(fp, 'utf-8')
|
|
|
|
>>> fp.close()
|
|
|
|
|
|
|
|
For simplifying the selection of a generator use ``feedgenerator.DefaultFeed``
|
|
|
|
which is currently ``Rss201rev2Feed``
|
|
|
|
|
|
|
|
For definitions of the different versions of RSS, see:
|
|
|
|
http://diveintomark.org/archives/2004/02/04/incompatible-rss
|
|
|
|
|
|
|
|
.. 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
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
See http://diveintomark.org/archives/2004/05/28/howto-atom-id
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
SyndicationFeed
|
|
|
|
---------------
|
|
|
|
|
|
|
|
.. class:: SyndicationFeed
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Base class for all syndication feeds. Subclasses should provide write().
|
2011-03-04 06:20:54 +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])
|
|
|
|
|
|
|
|
Initialize the feed with the given dictionary of metadata, which applies
|
|
|
|
to the entire feed.
|
|
|
|
|
|
|
|
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-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08: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, **kwargs])
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Adds an item to the feed. All args are expected to be Python ``unicode``
|
|
|
|
objects except ``pubdate``, which is a ``datetime.datetime`` object, and
|
|
|
|
``enclosure``, which is an instance of the ``Enclosure`` class.
|
2010-05-09 05:38:27 +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
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Returns the latest item's ``pubdate``. If none of them have a
|
|
|
|
``pubdate``, this returns the current date/time.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
Enclosure
|
|
|
|
---------
|
|
|
|
|
|
|
|
.. class:: Enclosure
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Represents an RSS enclosure
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
RssFeed
|
|
|
|
-------
|
|
|
|
|
|
|
|
.. class:: RssFeed(SyndicationFeed)
|
|
|
|
|
|
|
|
Rss201rev2Feed
|
|
|
|
--------------
|
|
|
|
|
|
|
|
.. class:: Rss201rev2Feed(RssFeed)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Spec: http://blogs.law.harvard.edu/tech/rss
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
RssUserland091Feed
|
|
|
|
------------------
|
|
|
|
|
|
|
|
.. class:: RssUserland091Feed(RssFeed)
|
|
|
|
|
|
|
|
Spec: http://backend.userland.com/rss091
|
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
Atom1Feed
|
|
|
|
---------
|
|
|
|
|
|
|
|
.. class:: Atom1Feed(SyndicationFeed)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Spec: http://atompub.org/2005/07/11/draft-ietf-atompub-format-10.html
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
``django.utils.http``
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. module:: django.utils.http
|
|
|
|
:synopsis: HTTP helper functions. (URL encoding, cookie handling, ...)
|
|
|
|
|
|
|
|
.. function:: urlquote(url, safe='/')
|
|
|
|
|
2011-02-16 09:56:53 +08: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-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: urlquote_plus(url, safe='')
|
|
|
|
|
2011-02-16 09:56:53 +08: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-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: urlencode(query, doseq=0)
|
|
|
|
|
2011-02-16 09:56:53 +08: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-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: cookie_date(epoch_seconds=None)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Formats the time to ensure compatibility with Netscape's cookie standard.
|
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:: http_date(epoch_seconds=None)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Formats the time to match the RFC 1123 date format as specified by HTTP
|
|
|
|
`RFC 2616`_ section 3.3.1.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
.. _RFC 2616: http://www.w3.org/Protocols/rfc2616/rfc2616.txt
|
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)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Converts a base 36 string to an integer.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: int_to_base36(i)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Converts an integer to a base 36 string.
|
2010-05-09 05:38:27 +08: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.
|
|
|
|
|
|
|
|
.. class:: SafeString
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
A string subclass that has been specifically marked as "safe" (requires no
|
|
|
|
further escaping) for HTML output purposes.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. class:: SafeUnicode
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
A unicode subclass that has been specifically marked as "safe" 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
|
|
|
|
object can be used everywhere a string or unicode object 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
|
|
|
|
|
|
|
.. function:: mark_for_escaping(s)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Explicitly mark a string as requiring HTML escaping upon output. Has no
|
|
|
|
effect on ``SafeData`` subclasses.
|
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 (the resulting escaping is
|
|
|
|
only applied once).
|
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
|
2010-08-20 03:27:44 +08:00
|
|
|
:doc:`Internationalization documentation </topics/i18n/internationalization>`.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: gettext(message)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Translates ``message`` and returns it in a UTF-8 bytestring
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: ugettext(message)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Translates ``message`` and returns it in a unicode string
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: gettext_lazy(message)
|
|
|
|
.. function:: ugettext_lazy(message)
|
|
|
|
|
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
|
|
|
|
based on ``number`` in a UTF-8 bytestring.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: ungettext(singular, plural, number)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Translates ``singular`` and ``plural`` and returns the appropriate string
|
|
|
|
based on ``number`` in a unicode string.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: ngettext_lazy(singular, plural, number)
|
|
|
|
.. function:: ungettext_lazy(singular, plural, number)
|
|
|
|
|
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:: string_concat(*strings)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Lazy variant of string concatenation, needed for translations that are
|
|
|
|
constructed from multiple parts.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: activate(language)
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Fetches the translation object for a given tuple of application name and
|
|
|
|
language and installs it as the current translation object for the current
|
|
|
|
thread.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: deactivate()
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
De-installs the currently active translation object so that further _ calls
|
|
|
|
will resolve against the default translation object, again.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: deactivate_all()
|
|
|
|
|
2011-02-16 09:56:53 +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
|
|
|
|
|
|
|
.. function:: get_language()
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Returns the currently selected language code.
|
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-02-16 09:56:53 +08:00
|
|
|
* ``False`` = left-to-right layout
|
|
|
|
* ``True`` = right-to-left layout
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: get_date_formats()
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Checks whether translation files provide a translation for some technical
|
|
|
|
message ID to store date and time formats. If it doesn't contain one, the
|
|
|
|
formats provided in the settings will be used.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. function:: get_language_from_request(request)
|
|
|
|
|
2011-02-16 09:56:53 +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
|
|
|
|
|
|
|
.. 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)
|
|
|
|
|
2011-02-16 09:56:53 +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
|
|
|
|
|
|
|
``django.utils.tzinfo``
|
|
|
|
=======================
|
|
|
|
|
|
|
|
.. module:: django.utils.tzinfo
|
|
|
|
:synopsis: Implementation of ``tzinfo`` classes for use with ``datetime.datetime``.
|
|
|
|
|
|
|
|
.. class:: FixedOffset
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Fixed offset in minutes east from UTC.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
.. class:: LocalTimezone
|
|
|
|
|
2011-02-16 09:56:53 +08:00
|
|
|
Proxy timezone information from time module.
|