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:
- 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.
Adds some useful headers to the given HttpResponse object:
- ETag
- Last-Modified
- Expires
- Cache-Control
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.
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:
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)])
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.
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.
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.
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 Internationalization documentation.
Same as the non-lazy versions above, but using lazy execution.
Same as the non-lazy versions above, but using lazy execution.
Returns selected language's BiDi layout:
- False = left-to-right layout
- True = right-to-left layout
Mar 31, 2011