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 internal release deprecation policy.
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.
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.
For example, internationalization middleware would need to distinguish caches by the Accept-language header.
This function patches the Cache-Control header by adding all keyword arguments to it. The transformation is as follows:
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).
Adds some useful headers to the given HttpResponse object:
Each header is only added if it isn’t already set.
cache_timeout is in seconds. The CACHE_MIDDLEWARE_SECONDS setting is used by default.
Adds headers to a response to indicate that a page should never be cached.
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.
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.
If there is no headerlist stored, the page needs to be rebuilt, so this function returns None.
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.
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.
The 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:
Inserts the key, value pair before the item with the given index.
Returns the value of the item at the given zero-based index.
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)])
The functions defined in this module share the following properties:
Parses a string and returns a datetime.date.
Parses a string and returns a datetime.time.
UTC offsets aren’t supported; if value describes one, the result is None.
Parses a string and returns a datetime.datetime.
UTC offsets are supported; if value describes one, the result’s tzinfo attribute is a FixedOffset instance.
A class whose __str__ returns its __unicode__ as a UTF-8 bytestring. Useful as a mix-in.
Returns a unicode object representing s. Treats bytestrings using the ‘encoding’ codec.
If strings_only is True, don’t convert (some) non-string-like objects.
Determine if the object instance is of a protected type.
Objects of protected types are preserved as-is when passed to force_unicode(strings_only=True).
Similar to smart_unicode, except that lazy instances are resolved to strings, rather than kept as lazy objects.
If strings_only is True, don’t convert (some) non-string-like objects.
Returns a bytestring version of s, encoded as specified in encoding.
If strings_only is True, don’t convert (some) non-string-like objects.
Convert an Internationalized Resource Identifier (IRI) portion to a URI portion that is suitable for inclusion in a URL.
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.
Returns an ASCII string containing the encoded result.
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",
... description=u"A group Weblog by the sharpest minds in online media/journalism/publishing.",
... 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
Creates a TagURI.
See http://diveintomark.org/archives/2004/05/28/howto-atom-id
Base class for all syndication feeds. Subclasses should provide write().
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.
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.
Return extra attributes to place on the root (i.e. feed/channel) element. Called from write().
Add elements in the root (i.e. feed/channel) element. Called from write().
Return extra attributes to place on each item (i.e. item/entry) element.
Add elements on each item (i.e. item/entry) element.
Outputs the feed in the given encoding to outfile, which is a file-like object. Subclasses should override this.
Returns the feed in the given encoding as a string.
Returns the latest item’s pubdate. If none of them have a pubdate, this returns the current date/time.
Spec: http://www.atomenabled.org/developers/syndication/atom-format-spec.php
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'
...
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 original function can return. Usually, it’s enough to include unicode here and ensure that your function returns only Unicode strings.
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.
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.
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.
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.
Formats the time to ensure compatibility with Netscape’s cookie standard.
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.
Outputs a string in the format Wdy, DD-Mon-YYYY HH:MM:SS GMT.
Formats the time to match the RFC 1123 date format as specified by HTTP RFC 2616 section 3.3.1.
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.
Outputs a string in the format Wdy, DD Mon YYYY HH:MM:SS GMT.
Converts a base 36 string to an integer.
Converts a positive integer less than sys.maxint to a base 36 string.
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.
A string subclass that has been specifically marked as “safe” (requires no further escaping) for HTML output purposes.
A unicode subclass that has been specifically marked as “safe” for HTML output purposes.
Explicitly mark a string as safe for (HTML) output purposes. The returned object can be used everywhere a string or unicode object is appropriate.
Can be called multiple times on a single string.
Explicitly mark a string as requiring HTML escaping upon output. Has no effect on SafeData subclasses.
Can be called multiple times on a single string (the resulting escaping is only applied once).
For a complete discussion on the usage of the following see the translation documentation.
Translates message and returns it in a UTF-8 bytestring
Translates message and returns it in a unicode string
Translates message given the context and returns it in a unicode string.
For more information, see Контекстные маркеры.
Same as the non-lazy versions above, but using lazy execution.
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.
Translates singular and plural and returns the appropriate string based on number in a UTF-8 bytestring.
Translates singular and plural and returns the appropriate string based on number in a unicode string.
Translates singular and plural and returns the appropriate string based on number and the context in a unicode string.
Same as the non-lazy versions above, but using lazy execution.
Lazy variant of string concatenation, needed for translations that are constructed from multiple parts.
Fetches the translation object for a given language and installs it as the current translation object for the current thread.
De-installs the currently active translation object so that further _ calls will resolve against the default translation object, again.
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.
A Python context manager that uses django.utils.translation.activate() to fetch the translation object for a given language, installing it as the translation object for the current thread and reinstall the previous active language on exit. Optionally it can simply deinstall the temporary translation on exit with 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.
Returns the currently selected language code.
Returns selected language’s BiDi layout:
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.
If check_path is True, the function first checks the requested URL for whether its path begins with a language code listed in the LANGUAGES setting.
Turns a language name (en-us) into a locale name (en_US).
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.
Returns a tzinfo instance that represents the default time zone.
Returns the name of the default time zone.
Returns a tzinfo instance that represents the current time zone.
Returns the name of the current time zone.
Sets the current time zone. The timezone argument must be an instance of a tzinfo subclass or, if pytz is available, a time zone name.
Unsets the current time zone.
This is a Python context manager that sets the current time zone on entry with activate(), and restores the previously active time zone on exit. If the timezone argument is None, the current time zone is unset on entry with deactivate() instead.
Returns an aware or naive datetime that represents the current point in time when USE_TZ is True or False respectively.
Returns True if value is aware, False if it is naive. This function assumes that value is a datetime.
Returns True if value is naive, False if it is aware. This function assumes that value is a datetime.
Mar 30, 2016