Django

Code

Changeset 6741

Show
Ignore:
Timestamp:
11/29/07 11:41:35 (9 months ago)
Author:
mtredinnick
Message:

Added better error handling in the basic feed class example. Refs #5855

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/syndication_feeds.txt

    r6570 r6741  
    202202An example makes this clear. Here's the code for these beat-specific feeds:: 
    203203 
     204    from django.contrib.syndication import FeedDoesNotExist 
     205 
    204206    class BeatFeed(Feed): 
    205207        def get_object(self, bits): 
     
    214216 
    215217        def link(self, obj): 
     218            if not obj: 
     219                raise FeedDoesNotExist 
    216220            return obj.get_absolute_url() 
    217221 
     
    247251      algorithm: 
    248252 
    249           * First, it tries to call a method, passing the ``obj`` argument, where 
    250             ``obj`` is the object returned by ``get_object()``. 
     253          * First, it tries to call a method, passing the ``obj`` argument, 
     254            where ``obj`` is the object returned by ``get_object()``. 
    251255          * Failing that, it tries to call a method with no arguments. 
    252256          * Failing that, it uses the class attribute. 
     257 
     258      Inside the ``link()`` method, we handle the possibility that ``obj`` 
     259      might be ``None``, which can occur when the URL isn't fully specified. In 
     260      some cases, you might want to do something else in this case, which would 
     261      mean you'd need to check for ``obj`` existing in other methods as well 
     262      (the ``link()`` method is called very early in the feed generation 
     263      process, so is a good place to bail out early). 
    253264 
    254265    * Finally, note that ``items()`` in this example also takes the ``obj``