Django

Code

root/django/branches/0.90-bugfixes/docs/syndication_feeds.txt

Revision 1228, 24.9 kB (checked in by adrian, 3 years ago)

Fixed #787 -- High-level syndication framework now picks up author details. Also updated documentation. Thanks, mattycakes

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