Kaydet (Commit) f86e54e3 authored tarafından Malcolm Tredinnick's avatar Malcolm Tredinnick

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


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6741 bcc190cf-cafb-0310-a4f2-bffc1f526a37
üst d94ed44c
...@@ -201,6 +201,8 @@ the feed. ...@@ -201,6 +201,8 @@ the feed.
An example makes this clear. Here's the code for these beat-specific feeds:: An example makes this clear. Here's the code for these beat-specific feeds::
from django.contrib.syndication import FeedDoesNotExist
class BeatFeed(Feed): class BeatFeed(Feed):
def get_object(self, bits): def get_object(self, bits):
# In case of "/rss/beats/0613/foo/bar/baz/", or other such clutter, # In case of "/rss/beats/0613/foo/bar/baz/", or other such clutter,
...@@ -213,6 +215,8 @@ An example makes this clear. Here's the code for these beat-specific feeds:: ...@@ -213,6 +215,8 @@ An example makes this clear. Here's the code for these beat-specific feeds::
return "Chicagocrime.org: Crimes for beat %s" % obj.beat return "Chicagocrime.org: Crimes for beat %s" % obj.beat
def link(self, obj): def link(self, obj):
if not obj:
raise FeedDoesNotExist
return obj.get_absolute_url() return obj.get_absolute_url()
def description(self, obj): def description(self, obj):
...@@ -246,11 +250,18 @@ request to the URL ``/rss/beats/0613/``: ...@@ -246,11 +250,18 @@ request to the URL ``/rss/beats/0613/``:
each of ``title``, ``link`` and ``description``, Django follows this each of ``title``, ``link`` and ``description``, Django follows this
algorithm: algorithm:
* First, it tries to call a method, passing the ``obj`` argument, where * First, it tries to call a method, passing the ``obj`` argument,
``obj`` is the object returned by ``get_object()``. where ``obj`` is the object returned by ``get_object()``.
* Failing that, it tries to call a method with no arguments. * Failing that, it tries to call a method with no arguments.
* Failing that, it uses the class attribute. * Failing that, it uses the class attribute.
Inside the ``link()`` method, we handle the possibility that ``obj``
might be ``None``, which can occur when the URL isn't fully specified. In
some cases, you might want to do something else in this case, which would
mean you'd need to check for ``obj`` existing in other methods as well
(the ``link()`` method is called very early in the feed generation
process, so is a good place to bail out early).
* Finally, note that ``items()`` in this example also takes the ``obj`` * Finally, note that ``items()`` in this example also takes the ``obj``
argument. The algorithm for ``items`` is the same as described in the argument. The algorithm for ``items`` is the same as described in the
previous step -- first, it tries ``items(obj)``, then ``items()``, then previous step -- first, it tries ``items(obj)``, then ``items()``, then
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment