syndication.txt 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. .. _ref-contrib-syndication:
  2. ==============================
  3. The syndication feed framework
  4. ==============================
  5. .. module:: django.contrib.syndication
  6. :synopsis: A framework for generating syndication feeds, in RSS and Atom,
  7. quite easily.
  8. Django comes with a high-level syndication-feed-generating framework that makes
  9. creating RSS_ and Atom_ feeds easy.
  10. To create any syndication feed, all you have to do is write a short Python
  11. class. You can create as many feeds as you want.
  12. Django also comes with a lower-level feed-generating API. Use this if you want
  13. to generate feeds outside of a Web context, or in some other lower-level way.
  14. .. _RSS: http://www.whatisrss.com/
  15. .. _Atom: http://www.atomenabled.org/
  16. The high-level framework
  17. ========================
  18. Overview
  19. --------
  20. The high-level feed-generating framework is a view that's hooked to ``/feeds/``
  21. by default. Django uses the remainder of the URL (everything after ``/feeds/``)
  22. to determine which feed to output.
  23. To create a feed, just write a :class:`~django.contrib.syndication.feeds.Feed`
  24. class and point to it in your :ref:`URLconf <topics-http-urls>`.
  25. Initialization
  26. --------------
  27. To activate syndication feeds on your Django site, add this line to your
  28. :ref:`URLconf <topics-http-urls>`::
  29. (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds}),
  30. This tells Django to use the RSS framework to handle all URLs starting with
  31. :file:`"feeds/"`. (You can change that :file:`"feeds/"` prefix to fit your own
  32. needs.)
  33. This URLconf line has an extra argument: ``{'feed_dict': feeds}``. Use this
  34. extra argument to pass the syndication framework the feeds that should be
  35. published under that URL.
  36. Specifically, :data:`feed_dict` should be a dictionary that maps a feed's slug
  37. (short URL label) to its :class:`~django.contrib.syndication.feeds.Feed` class.
  38. You can define the ``feed_dict`` in the URLconf itself. Here's a full example
  39. URLconf::
  40. from django.conf.urls.defaults import *
  41. from myproject.feeds import LatestEntries, LatestEntriesByCategory
  42. feeds = {
  43. 'latest': LatestEntries,
  44. 'categories': LatestEntriesByCategory,
  45. }
  46. urlpatterns = patterns('',
  47. # ...
  48. (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
  49. {'feed_dict': feeds}),
  50. # ...
  51. )
  52. The above example registers two feeds:
  53. * The feed represented by ``LatestEntries`` will live at ``feeds/latest/``.
  54. * The feed represented by ``LatestEntriesByCategory`` will live at
  55. ``feeds/categories/``.
  56. Once that's set up, you just need to define the
  57. :class:`~django.contrib.syndication.feeds.Feed` classes themselves.
  58. Feed classes
  59. ------------
  60. A :class:`~django.contrib.syndication.feeds.Feed` class is a simple Python class
  61. that represents a syndication feed. A feed can be simple (e.g., a "site news"
  62. feed, or a basic feed displaying the latest entries of a blog) or more complex
  63. (e.g., a feed displaying all the blog entries in a particular category, where
  64. the category is variable).
  65. :class:`~django.contrib.syndication.feeds.Feed` classes must subclass
  66. ``django.contrib.syndication.feeds.Feed``. They can live anywhere in your
  67. codebase.
  68. A simple example
  69. ----------------
  70. This simple example, taken from `chicagocrime.org`_, describes a feed of the
  71. latest five news items::
  72. from django.contrib.syndication.feeds import Feed
  73. from chicagocrime.models import NewsItem
  74. class LatestEntries(Feed):
  75. title = "Chicagocrime.org site news"
  76. link = "/sitenews/"
  77. description = "Updates on changes and additions to chicagocrime.org."
  78. def items(self):
  79. return NewsItem.objects.order_by('-pub_date')[:5]
  80. Note:
  81. * The class subclasses ``django.contrib.syndication.feeds.Feed``.
  82. * :attr:`title`, :attr:`link` and :attr:`description` correspond to the
  83. standard RSS ``<title>``, ``<link>`` and ``<description>`` elements,
  84. respectively.
  85. * :meth:`items()` is, simply, a method that returns a list of objects that
  86. should be included in the feed as ``<item>`` elements. Although this
  87. example returns ``NewsItem`` objects using Django's
  88. :ref:`object-relational mapper <ref-models-querysets>`, :meth:`items()`
  89. doesn't have to return model instances. Although you get a few bits of
  90. functionality "for free" by using Django models, :meth:`items()` can
  91. return any type of object you want.
  92. * If you're creating an Atom feed, rather than an RSS feed, set the
  93. :attr:`subtitle` attribute instead of the :attr:`description` attribute.
  94. See `Publishing Atom and RSS feeds in tandem`_, later, for an example.
  95. One thing's left to do. In an RSS feed, each ``<item>`` has a ``<title>``,
  96. ``<link>`` and ``<description>``. We need to tell the framework what data to put
  97. into those elements.
  98. * To specify the contents of ``<title>`` and ``<description>``, create
  99. :ref:`Django templates <topics-templates>` called
  100. :file:`feeds/latest_title.html` and
  101. :file:`feeds/latest_description.html`, where :attr:`latest` is the
  102. :attr:`slug` specified in the URLconf for the given feed. Note the
  103. ``.html`` extension is required. The RSS system renders that template for
  104. each item, passing it two template context variables:
  105. * ``{{ obj }}`` -- The current object (one of whichever objects you
  106. returned in :meth:`items()`).
  107. * ``{{ site }}`` -- A :class:`django.contrib.sites.models.Site` object
  108. representing the current site. This is useful for ``{{ site.domain
  109. }}`` or ``{{ site.name }}``. If you do *not* have the Django sites
  110. framework installed, this will be set to a
  111. :class:`django.contrib.sites.models.RequestSite` object. See the
  112. :ref:`RequestSite section of the sites framework documentation
  113. <requestsite-objects>` for more.
  114. If you don't create a template for either the title or description, the
  115. framework will use the template ``"{{ obj }}"`` by default -- that is, the
  116. normal string representation of the object. You can also change the names
  117. of these two templates by specifying ``title_template`` and
  118. ``description_template`` as attributes of your
  119. :class:`~django.contrib.syndication.feeds.Feed` class.
  120. * To specify the contents of ``<link>``, you have two options. For each item
  121. in :meth:`items()`, Django first tries calling a method
  122. :meth:`item_link()` in the :class:`~django.contrib.syndication.feeds.Feed`
  123. class, passing it a single parameter, :attr:`item`, which is the object
  124. itself. If that method doesn't exist, Django tries executing a
  125. ``get_absolute_url()`` method on that object. . Both
  126. ``get_absolute_url()`` and :meth:`item_link()` should return the item's
  127. URL as a normal Python string. As with ``get_absolute_url()``, the result
  128. of :meth:`item_link()` will be included directly in the URL, so you are
  129. responsible for doing all necessary URL quoting and conversion to ASCII
  130. inside the method itself.
  131. * For the LatestEntries example above, we could have very simple feed
  132. templates:
  133. * latest_title.html:
  134. .. code-block:: html+django
  135. {{ obj.title }}
  136. * latest_description.html:
  137. .. code-block:: html+django
  138. {{ obj.description }}
  139. .. _chicagocrime.org: http://www.chicagocrime.org/
  140. A complex example
  141. -----------------
  142. The framework also supports more complex feeds, via parameters.
  143. For example, `chicagocrime.org`_ offers an RSS feed of recent crimes for every
  144. police beat in Chicago. It'd be silly to create a separate
  145. :class:`~django.contrib.syndication.feeds.Feed` class for each police beat; that
  146. would violate the :ref:`DRY principle <dry>` and would couple data to
  147. programming logic. Instead, the syndication framework lets you make generic
  148. feeds that output items based on information in the feed's URL.
  149. On chicagocrime.org, the police-beat feeds are accessible via URLs like this:
  150. * :file:`/rss/beats/0613/` -- Returns recent crimes for beat 0613.
  151. * :file:`/rss/beats/1424/` -- Returns recent crimes for beat 1424.
  152. The slug here is ``"beats"``. The syndication framework sees the extra URL bits
  153. after the slug -- ``0613`` and ``1424`` -- and gives you a hook to tell it what
  154. those URL bits mean, and how they should influence which items get published in
  155. the feed.
  156. An example makes this clear. Here's the code for these beat-specific feeds::
  157. from django.contrib.syndication.feeds import FeedDoesNotExist
  158. from django.core.exceptions import ObjectDoesNotExist
  159. class BeatFeed(Feed):
  160. def get_object(self, bits):
  161. # In case of "/rss/beats/0613/foo/bar/baz/", or other such clutter,
  162. # check that bits has only one member.
  163. if len(bits) != 1:
  164. raise ObjectDoesNotExist
  165. return Beat.objects.get(beat__exact=bits[0])
  166. def title(self, obj):
  167. return "Chicagocrime.org: Crimes for beat %s" % obj.beat
  168. def link(self, obj):
  169. if not obj:
  170. raise FeedDoesNotExist
  171. return obj.get_absolute_url()
  172. def description(self, obj):
  173. return "Crimes recently reported in police beat %s" % obj.beat
  174. def items(self, obj):
  175. return Crime.objects.filter(beat__id__exact=obj.id).order_by('-crime_date')[:30]
  176. Here's the basic algorithm the RSS framework follows, given this class and a
  177. request to the URL :file:`/rss/beats/0613/`:
  178. * The framework gets the URL :file:`/rss/beats/0613/` and notices there's an
  179. extra bit of URL after the slug. It splits that remaining string by the
  180. slash character (``"/"``) and calls the
  181. :class:`~django.contrib.syndication.feeds.Feed` class'
  182. :meth:`get_object()` method, passing it the bits. In this case, bits is
  183. ``['0613']``. For a request to :file:`/rss/beats/0613/foo/bar/`, bits
  184. would be ``['0613', 'foo', 'bar']``.
  185. * :meth:`get_object()` is responsible for retrieving the given beat, from
  186. the given ``bits``. In this case, it uses the Django database API to
  187. retrieve the beat. Note that :meth:`get_object()` should raise
  188. :exc:`django.core.exceptions.ObjectDoesNotExist` if given invalid
  189. parameters. There's no ``try``/``except`` around the
  190. ``Beat.objects.get()`` call, because it's not necessary; that function
  191. raises :exc:`Beat.DoesNotExist` on failure, and :exc:`Beat.DoesNotExist`
  192. is a subclass of :exc:`ObjectDoesNotExist`. Raising
  193. :exc:`ObjectDoesNotExist` in :meth:`get_object()` tells Django to produce
  194. a 404 error for that request.
  195. .. versionadded:: 1.0
  196. :meth:`get_object()` can handle the :file:`/rss/beats/` url.
  197. The :meth:`get_object()` method also has a chance to handle the
  198. :file:`/rss/beats/` url. In this case, :data:`bits` will be an
  199. empty list. In our example, ``len(bits) != 1`` and an
  200. :exc:`ObjectDoesNotExist` exception will be raised, so
  201. :file:`/rss/beats/` will generate a 404 page. But you can handle this case
  202. however you like. For example, you could generate a combined feed for all
  203. beats.
  204. * To generate the feed's ``<title>``, ``<link>`` and ``<description>``,
  205. Django uses the :meth:`title()`, :meth:`link()` and :meth:`description()`
  206. methods. In the previous example, they were simple string class
  207. attributes, but this example illustrates that they can be either strings
  208. *or* methods. For each of :attr:`title`, :attr:`link` and
  209. :attr:`description`, Django follows this algorithm:
  210. * First, it tries to call a method, passing the ``obj`` argument, where
  211. ``obj`` is the object returned by :meth:`get_object()`.
  212. * Failing that, it tries to call a method with no arguments.
  213. * Failing that, it uses the class attribute.
  214. Inside the :meth:`link()` method, we handle the possibility that ``obj``
  215. might be ``None``, which can occur when the URL isn't fully specified. In
  216. some cases, you might want to do something else in this case, which would
  217. mean you'd need to check for ``obj`` existing in other methods as well.
  218. (The :meth:`link()` method is called very early in the feed generation
  219. process, so it's a good place to bail out early.)
  220. * Finally, note that :meth:`items()` in this example also takes the ``obj``
  221. argument. The algorithm for :attr:`items` is the same as described in the
  222. previous step -- first, it tries :meth:`items(obj)`, then :meth:`items()`,
  223. then finally an :attr:`items` class attribute (which should be a list).
  224. The ``ExampleFeed`` class below gives full documentation on methods and
  225. attributes of :class:`~django.contrib.syndication.feeds.Feed` classes.
  226. Specifying the type of feed
  227. ---------------------------
  228. By default, feeds produced in this framework use RSS 2.0.
  229. To change that, add a ``feed_type`` attribute to your
  230. :class:`~django.contrib.syndication.feeds.Feed` class, like so::
  231. from django.utils.feedgenerator import Atom1Feed
  232. class MyFeed(Feed):
  233. feed_type = Atom1Feed
  234. Note that you set ``feed_type`` to a class object, not an instance.
  235. Currently available feed types are:
  236. * :class:`django.utils.feedgenerator.Rss201rev2Feed` (RSS 2.01. Default.)
  237. * :class:`django.utils.feedgenerator.RssUserland091Feed` (RSS 0.91.)
  238. * :class:`django.utils.feedgenerator.Atom1Feed` (Atom 1.0.)
  239. Enclosures
  240. ----------
  241. To specify enclosures, such as those used in creating podcast feeds, use the
  242. :attr:`item_enclosure_url`, :attr:`item_enclosure_length` and
  243. :attr:`item_enclosure_mime_type` hooks. See the ``ExampleFeed`` class below for
  244. usage examples.
  245. Language
  246. --------
  247. Feeds created by the syndication framework automatically include the
  248. appropriate ``<language>`` tag (RSS 2.0) or ``xml:lang`` attribute (Atom). This
  249. comes directly from your :setting:`LANGUAGE_CODE setting`.
  250. URLs
  251. ----
  252. The :attr:`link` method/attribute can return either an absolute URL (e.g.
  253. :file:`"/blog/"`) or a URL with the fully-qualified domain and protocol (e.g.
  254. ``"http://www.example.com/blog/"``). If :attr:`link` doesn't return the domain,
  255. the syndication framework will insert the domain of the current site, according
  256. to your :setting:`SITE_ID setting <SITE_ID>`.
  257. Atom feeds require a ``<link rel="self">`` that defines the feed's current
  258. location. The syndication framework populates this automatically, using the
  259. domain of the current site according to the :setting:`SITE_ID` setting.
  260. Publishing Atom and RSS feeds in tandem
  261. ---------------------------------------
  262. Some developers like to make available both Atom *and* RSS versions of their
  263. feeds. That's easy to do with Django: Just create a subclass of your
  264. :class:`~django.contrib.syndication.feeds.Feed`
  265. class and set the :attr:`feed_type` to something different. Then update your
  266. URLconf to add the extra versions.
  267. Here's a full example::
  268. from django.contrib.syndication.feeds import Feed
  269. from chicagocrime.models import NewsItem
  270. from django.utils.feedgenerator import Atom1Feed
  271. class RssSiteNewsFeed(Feed):
  272. title = "Chicagocrime.org site news"
  273. link = "/sitenews/"
  274. description = "Updates on changes and additions to chicagocrime.org."
  275. def items(self):
  276. return NewsItem.objects.order_by('-pub_date')[:5]
  277. class AtomSiteNewsFeed(RssSiteNewsFeed):
  278. feed_type = Atom1Feed
  279. subtitle = RssSiteNewsFeed.description
  280. .. Note::
  281. In this example, the RSS feed uses a :attr:`description` while the Atom
  282. feed uses a :attr:`subtitle`. That's because Atom feeds don't provide for
  283. a feed-level "description," but they *do* provide for a "subtitle."
  284. If you provide a :attr:`description` in your
  285. :class:`~django.contrib.syndication.feeds.Feed` class, Django will *not*
  286. automatically put that into the :attr:`subtitle` element, because a
  287. subtitle and description are not necessarily the same thing. Instead, you
  288. should define a :attr:`subtitle` attribute.
  289. In the above example, we simply set the Atom feed's :attr:`subtitle` to the
  290. RSS feed's :attr:`description`, because it's quite short already.
  291. And the accompanying URLconf::
  292. from django.conf.urls.defaults import *
  293. from myproject.feeds import RssSiteNewsFeed, AtomSiteNewsFeed
  294. feeds = {
  295. 'rss': RssSiteNewsFeed,
  296. 'atom': AtomSiteNewsFeed,
  297. }
  298. urlpatterns = patterns('',
  299. # ...
  300. (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
  301. {'feed_dict': feeds}),
  302. # ...
  303. )
  304. Feed class reference
  305. --------------------
  306. .. class:: django.contrib.syndication.feeds.Feed
  307. This example illustrates all possible attributes and methods for a
  308. :class:`~django.contrib.syndication.feeds.Feed` class::
  309. from django.contrib.syndication.feeds import Feed
  310. from django.utils import feedgenerator
  311. class ExampleFeed(Feed):
  312. # FEED TYPE -- Optional. This should be a class that subclasses
  313. # django.utils.feedgenerator.SyndicationFeed. This designates which
  314. # type of feed this should be: RSS 2.0, Atom 1.0, etc.
  315. # If you don't specify feed_type, your feed will be RSS 2.0.
  316. # This should be a class, not an instance of the class.
  317. feed_type = feedgenerator.Rss201rev2Feed
  318. # TEMPLATE NAMES -- Optional. These should be strings representing
  319. # names of Django templates that the system should use in rendering the
  320. # title and description of your feed items. Both are optional.
  321. # If you don't specify one, or either, Django will use the template
  322. # 'feeds/SLUG_title.html' and 'feeds/SLUG_description.html', where SLUG
  323. # is the slug you specify in the URL.
  324. title_template = None
  325. description_template = None
  326. # TITLE -- One of the following three is required. The framework looks
  327. # for them in this order.
  328. def title(self, obj):
  329. """
  330. Takes the object returned by get_object() and returns the feed's
  331. title as a normal Python string.
  332. """
  333. def title(self):
  334. """
  335. Returns the feed's title as a normal Python string.
  336. """
  337. title = 'foo' # Hard-coded title.
  338. # LINK -- One of the following three is required. The framework looks
  339. # for them in this order.
  340. def link(self, obj):
  341. """
  342. Takes the object returned by get_object() and returns the feed's
  343. link as a normal Python string.
  344. """
  345. def link(self):
  346. """
  347. Returns the feed's link as a normal Python string.
  348. """
  349. link = '/foo/bar/' # Hard-coded link.
  350. # GUID -- One of the following three is optional. The framework looks
  351. # for them in this order. This property is only used for Atom feeds
  352. # (where it is the feed-level ID element). If not provided, the feed
  353. # link is used as the ID.
  354. def feed_guid(self, obj):
  355. """
  356. Takes the object returned by get_object() and returns the globally
  357. unique ID for the feed as a normal Python string.
  358. """
  359. def feed_guid(self):
  360. """
  361. Returns the feed's globally unique ID as a normal Python string.
  362. """
  363. feed_guid = '/foo/bar/1234' # Hard-coded guid.
  364. # DESCRIPTION -- One of the following three is required. The framework
  365. # looks for them in this order.
  366. def description(self, obj):
  367. """
  368. Takes the object returned by get_object() and returns the feed's
  369. description as a normal Python string.
  370. """
  371. def description(self):
  372. """
  373. Returns the feed's description as a normal Python string.
  374. """
  375. description = 'Foo bar baz.' # Hard-coded description.
  376. # AUTHOR NAME --One of the following three is optional. The framework
  377. # looks for them in this order.
  378. def author_name(self, obj):
  379. """
  380. Takes the object returned by get_object() and returns the feed's
  381. author's name as a normal Python string.
  382. """
  383. def author_name(self):
  384. """
  385. Returns the feed's author's name as a normal Python string.
  386. """
  387. author_name = 'Sally Smith' # Hard-coded author name.
  388. # AUTHOR E-MAIL --One of the following three is optional. The framework
  389. # looks for them in this order.
  390. def author_email(self, obj):
  391. """
  392. Takes the object returned by get_object() and returns the feed's
  393. author's e-mail as a normal Python string.
  394. """
  395. def author_email(self):
  396. """
  397. Returns the feed's author's e-mail as a normal Python string.
  398. """
  399. author_email = 'test@example.com' # Hard-coded author e-mail.
  400. # AUTHOR LINK --One of the following three is optional. The framework
  401. # looks for them in this order. In each case, the URL should include
  402. # the "http://" and domain name.
  403. def author_link(self, obj):
  404. """
  405. Takes the object returned by get_object() and returns the feed's
  406. author's URL as a normal Python string.
  407. """
  408. def author_link(self):
  409. """
  410. Returns the feed's author's URL as a normal Python string.
  411. """
  412. author_link = 'http://www.example.com/' # Hard-coded author URL.
  413. # CATEGORIES -- One of the following three is optional. The framework
  414. # looks for them in this order. In each case, the method/attribute
  415. # should return an iterable object that returns strings.
  416. def categories(self, obj):
  417. """
  418. Takes the object returned by get_object() and returns the feed's
  419. categories as iterable over strings.
  420. """
  421. def categories(self):
  422. """
  423. Returns the feed's categories as iterable over strings.
  424. """
  425. categories = ("python", "django") # Hard-coded list of categories.
  426. # COPYRIGHT NOTICE -- One of the following three is optional. The
  427. # framework looks for them in this order.
  428. def copyright(self, obj):
  429. """
  430. Takes the object returned by get_object() and returns the feed's
  431. copyright notice as a normal Python string.
  432. """
  433. def copyright(self):
  434. """
  435. Returns the feed's copyright notice as a normal Python string.
  436. """
  437. copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice.
  438. # TTL -- One of the following three is optional. The framework looks
  439. # for them in this order. Ignored for Atom feeds.
  440. def ttl(self, obj):
  441. """
  442. Takes the object returned by get_object() and returns the feed's
  443. TTL (Time To Live) as a normal Python string.
  444. """
  445. def ttl(self):
  446. """
  447. Returns the feed's TTL as a normal Python string.
  448. """
  449. ttl = 600 # Hard-coded Time To Live.
  450. # ITEMS -- One of the following three is required. The framework looks
  451. # for them in this order.
  452. def items(self, obj):
  453. """
  454. Takes the object returned by get_object() and returns a list of
  455. items to publish in this feed.
  456. """
  457. def items(self):
  458. """
  459. Returns a list of items to publish in this feed.
  460. """
  461. items = ('Item 1', 'Item 2') # Hard-coded items.
  462. # GET_OBJECT -- This is required for feeds that publish different data
  463. # for different URL parameters. (See "A complex example" above.)
  464. def get_object(self, bits):
  465. """
  466. Takes a list of strings gleaned from the URL and returns an object
  467. represented by this feed. Raises
  468. django.core.exceptions.ObjectDoesNotExist on error.
  469. """
  470. # ITEM LINK -- One of these three is required. The framework looks for
  471. # them in this order.
  472. # First, the framework tries the two methods below, in
  473. # order. Failing that, it falls back to the get_absolute_url()
  474. # method on each item returned by items().
  475. def item_link(self, item):
  476. """
  477. Takes an item, as returned by items(), and returns the item's URL.
  478. """
  479. def item_link(self):
  480. """
  481. Returns the URL for every item in the feed.
  482. """
  483. # ITEM_GUID -- The following method is optional. If not provided, the
  484. # item's link is used by default.
  485. def item_guid(self, obj):
  486. """
  487. Takes an item, as return by items(), and returns the item's ID.
  488. """
  489. # ITEM AUTHOR NAME -- One of the following three is optional. The
  490. # framework looks for them in this order.
  491. def item_author_name(self, item):
  492. """
  493. Takes an item, as returned by items(), and returns the item's
  494. author's name as a normal Python string.
  495. """
  496. def item_author_name(self):
  497. """
  498. Returns the author name for every item in the feed.
  499. """
  500. item_author_name = 'Sally Smith' # Hard-coded author name.
  501. # ITEM AUTHOR E-MAIL --One of the following three is optional. The
  502. # framework looks for them in this order.
  503. #
  504. # If you specify this, you must specify item_author_name.
  505. def item_author_email(self, obj):
  506. """
  507. Takes an item, as returned by items(), and returns the item's
  508. author's e-mail as a normal Python string.
  509. """
  510. def item_author_email(self):
  511. """
  512. Returns the author e-mail for every item in the feed.
  513. """
  514. item_author_email = 'test@example.com' # Hard-coded author e-mail.
  515. # ITEM AUTHOR LINK --One of the following three is optional. The
  516. # framework looks for them in this order. In each case, the URL should
  517. # include the "http://" and domain name.
  518. #
  519. # If you specify this, you must specify item_author_name.
  520. def item_author_link(self, obj):
  521. """
  522. Takes an item, as returned by items(), and returns the item's
  523. author's URL as a normal Python string.
  524. """
  525. def item_author_link(self):
  526. """
  527. Returns the author URL for every item in the feed.
  528. """
  529. item_author_link = 'http://www.example.com/' # Hard-coded author URL.
  530. # ITEM ENCLOSURE URL -- One of these three is required if you're
  531. # publishing enclosures. The framework looks for them in this order.
  532. def item_enclosure_url(self, item):
  533. """
  534. Takes an item, as returned by items(), and returns the item's
  535. enclosure URL.
  536. """
  537. def item_enclosure_url(self):
  538. """
  539. Returns the enclosure URL for every item in the feed.
  540. """
  541. item_enclosure_url = "/foo/bar.mp3" # Hard-coded enclosure link.
  542. # ITEM ENCLOSURE LENGTH -- One of these three is required if you're
  543. # publishing enclosures. The framework looks for them in this order.
  544. # In each case, the returned value should be either an integer, or a
  545. # string representation of the integer, in bytes.
  546. def item_enclosure_length(self, item):
  547. """
  548. Takes an item, as returned by items(), and returns the item's
  549. enclosure length.
  550. """
  551. def item_enclosure_length(self):
  552. """
  553. Returns the enclosure length for every item in the feed.
  554. """
  555. item_enclosure_length = 32000 # Hard-coded enclosure length.
  556. # ITEM ENCLOSURE MIME TYPE -- One of these three is required if you're
  557. # publishing enclosures. The framework looks for them in this order.
  558. def item_enclosure_mime_type(self, item):
  559. """
  560. Takes an item, as returned by items(), and returns the item's
  561. enclosure MIME type.
  562. """
  563. def item_enclosure_mime_type(self):
  564. """
  565. Returns the enclosure MIME type for every item in the feed.
  566. """
  567. item_enclosure_mime_type = "audio/mpeg" # Hard-coded enclosure MIME type.
  568. # ITEM PUBDATE -- It's optional to use one of these three. This is a
  569. # hook that specifies how to get the pubdate for a given item.
  570. # In each case, the method/attribute should return a Python
  571. # datetime.datetime object.
  572. def item_pubdate(self, item):
  573. """
  574. Takes an item, as returned by items(), and returns the item's
  575. pubdate.
  576. """
  577. def item_pubdate(self):
  578. """
  579. Returns the pubdate for every item in the feed.
  580. """
  581. item_pubdate = datetime.datetime(2005, 5, 3) # Hard-coded pubdate.
  582. # ITEM CATEGORIES -- It's optional to use one of these three. This is
  583. # a hook that specifies how to get the list of categories for a given
  584. # item. In each case, the method/attribute should return an iterable
  585. # object that returns strings.
  586. def item_categories(self, item):
  587. """
  588. Takes an item, as returned by items(), and returns the item's
  589. categories.
  590. """
  591. def item_categories(self):
  592. """
  593. Returns the categories for every item in the feed.
  594. """
  595. item_categories = ("python", "django") # Hard-coded categories.
  596. # ITEM COPYRIGHT NOTICE (only applicable to Atom feeds) -- One of the
  597. # following three is optional. The framework looks for them in this
  598. # order.
  599. def item_copyright(self, obj):
  600. """
  601. Takes an item, as returned by items(), and returns the item's
  602. copyright notice as a normal Python string.
  603. """
  604. def item_copyright(self):
  605. """
  606. Returns the copyright notice for every item in the feed.
  607. """
  608. item_copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice.
  609. The low-level framework
  610. =======================
  611. Behind the scenes, the high-level RSS framework uses a lower-level framework
  612. for generating feeds' XML. This framework lives in a single module:
  613. `django/utils/feedgenerator.py`_.
  614. You use this framework on your own, for lower-level feed generation. You can
  615. also create custom feed generator subclasses for use with the ``feed_type``
  616. ``Feed`` option.
  617. ``SyndicationFeed`` classes
  618. ---------------------------
  619. The :mod:`~django.utils.feedgenerator` module contains a base class:
  620. .. class:: django.utils.feedgenerator.SyndicationFeed
  621. and several subclasses:
  622. .. class:: django.utils.feedgenerator.RssUserland091Feed
  623. .. class:: django.utils.feedgenerator.Rss201rev2Feed
  624. .. class:: django.utils.feedgenerator.Atom1Feed
  625. Each of these three classes knows how to render a certain type of feed as XML.
  626. They share this interface:
  627. .. method:: SyndicationFeed.__init__(**kwargs)
  628. Initialize the feed with the given dictionary of metadata, which applies to
  629. the entire feed. Required keyword arguments are:
  630. * ``title``
  631. * ``link``
  632. * ``description``
  633. There's also a bunch of other optional keywords:
  634. * ``language``
  635. * ``author_email``
  636. * ``author_name``
  637. * ``author_link``
  638. * ``subtitle``
  639. * ``categories``
  640. * ``feed_url``
  641. * ``feed_copyright``
  642. * ``feed_guid``
  643. * ``ttl``
  644. Any extra keyword arguments you pass to ``__init__`` will be stored in
  645. ``self.feed`` for use with `custom feed generators`_.
  646. All parameters should be Unicode objects, except ``categories``, which
  647. should be a sequence of Unicode objects.
  648. .. method:: SyndicationFeed.add_item(**kwargs)
  649. Add an item to the feed with the given parameters.
  650. Required keyword arguments are:
  651. * ``title``
  652. * ``link``
  653. * ``description``
  654. Optional keyword arguments are:
  655. * ``author_email``
  656. * ``author_name``
  657. * ``author_link``
  658. * ``pubdate``
  659. * ``comments``
  660. * ``unique_id``
  661. * ``enclosure``
  662. * ``categories``
  663. * ``item_copyright``
  664. * ``ttl``
  665. Extra keyword arguments will be stored for `custom feed generators`_.
  666. All parameters, if given, should be Unicode objects, except:
  667. * ``pubdate`` should be a `Python datetime object`_.
  668. * ``enclosure`` should be an instance of ``feedgenerator.Enclosure``.
  669. * ``categories`` should be a sequence of Unicode objects.
  670. .. method:: SyndicationFeed.write(outfile, encoding)
  671. Outputs the feed in the given encoding to outfile, which is a file-like object.
  672. .. method:: SyndicationFeed.writeString(encoding)
  673. Returns the feed as a string in the given encoding.
  674. For example, to create an Atom 1.0 feed and print it to standard output::
  675. >>> from django.utils import feedgenerator
  676. >>> f = feedgenerator.Atom1Feed(
  677. ... title=u"My Weblog",
  678. ... link=u"http://www.example.com/",
  679. ... description=u"In which I write about what I ate today.",
  680. ... language=u"en")
  681. >>> f.add_item(title=u"Hot dog today",
  682. ... link=u"http://www.example.com/entries/1/",
  683. ... description=u"<p>Today I had a Vienna Beef hot dog. It was pink, plump and perfect.</p>")
  684. >>> print f.writeString('UTF-8')
  685. <?xml version="1.0" encoding="UTF-8"?>
  686. <feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  687. ...
  688. </feed>
  689. .. _django/utils/feedgenerator.py: http://code.djangoproject.com/browser/django/trunk/django/utils/feedgenerator.py
  690. .. _Python datetime object: http://docs.python.org/library/datetime.html#datetime-objects
  691. Custom feed generators
  692. ----------------------
  693. If you need to produce a custom feed format, you've got a couple of options.
  694. If the feed format is totally custom, you'll want to subclass
  695. ``SyndicationFeed`` and completely replace the ``write()`` and
  696. ``writeString()`` methods.
  697. However, if the feed format is a spin-off of RSS or Atom (i.e. GeoRSS_, Apple's
  698. `iTunes podcast format`_, etc.), you've got a better choice. These types of
  699. feeds typically add extra elements and/or attributes to the underlying format,
  700. and there are a set of methods that ``SyndicationFeed`` calls to get these extra
  701. attributes. Thus, you can subclass the appropriate feed generator class
  702. (``Atom1Feed`` or ``Rss201rev2Feed``) and extend these callbacks. They are:
  703. .. _georss: http://georss.org/
  704. .. _itunes podcast format: http://www.apple.com/itunes/store/podcaststechspecs.html
  705. ``SyndicationFeed.root_attributes(self, )``
  706. Return a ``dict`` of attributes to add to the root feed element
  707. (``feed``/``channel``).
  708. ``SyndicationFeed.add_root_elements(self, handler)``
  709. Callback to add elements inside the root feed element
  710. (``feed``/``channel``). ``handler`` is an `XMLGenerator`_ from Python's
  711. built-in SAX library; you'll call methods on it to add to the XML
  712. document in process.
  713. ``SyndicationFeed.item_attributes(self, item)``
  714. Return a ``dict`` of attributes to add to each item (``item``/``entry``)
  715. element. The argument, ``item``, is a dictionary of all the data passed to
  716. ``SyndicationFeed.add_item()``.
  717. ``SyndicationFeed.add_item_elements(self, handler, item)``
  718. Callback to add elements to each item (``item``/``entry``) element.
  719. ``handler`` and ``item`` are as above.
  720. .. warning::
  721. If you override any of these methods, be sure to call the superclass methods
  722. since they add the required elements for each feed format.
  723. For example, you might start implementing an iTunes RSS feed generator like so::
  724. class iTunesFeed(Rss201rev2Feed):
  725. def root_attributes(self):
  726. attrs = super(iTunesFeed, self).root_attributes()
  727. attrs['xmlns:itunes'] = 'http://www.itunes.com/dtds/podcast-1.0.dtd'
  728. return attrs
  729. def add_root_elements(self, handler):
  730. super(iTunesFeed, self).add_root_elements(handler)
  731. handler.addQuickElement('itunes:explicit', 'clean')
  732. Obviously there's a lot more work to be done for a complete custom feed class,
  733. but the above example should demonstrate the basic idea.
  734. .. _XMLGenerator: http://docs.python.org/dev/library/xml.sax.utils.html#xml.sax.saxutils.XMLGenerator