Ticket #3624: feeds.2.diff

File feeds.2.diff, 4.7 KB (added by Rob Hudson <treborhudson@…>, 9 years ago)

Cleaned up, tested override and default, and added docs.

  • django/contrib/syndication/feeds.py

     
    8585        try:
    8686            title_tmp = loader.get_template(self.title_template_name)
    8787        except TemplateDoesNotExist:
    88             title_tmp = Template('{{ obj }}')
     88            title_tmp = self.__get_dynamic_attr('item_title', obj, Template('{{ obj }}'))
    8989        try:
    9090            description_tmp = loader.get_template(self.description_template_name)
    9191        except TemplateDoesNotExist:
    92             description_tmp = Template('{{ obj }}')
     92            description_tmp = self.__get_dynamic_attr('item_description', obj, Template('{{ obj }}')))
    9393
    9494        for item in self.__get_dynamic_attr('items', obj):
    9595            link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item))
  • docs/syndication_feeds.txt

     
    9393latest five news items::
    9494
    9595    from django.contrib.syndication.feeds import Feed
     96    from django.template import Template
    9697    from chicagocrime.models import NewsItem
    9798
    9899    class LatestEntries(Feed):
     
    103104        def items(self):
    104105            return NewsItem.objects.order_by('-pub_date')[:5]
    105106
     107        def item_description(self):
     108            return Template('{{ obj.body }}')
     109
    106110Note:
    107111
    108112    * The class subclasses ``django.contrib.syndication.feeds.Feed``.
     
    114118      `object-relational mapper`_, ``items()`` doesn't have to return model
    115119      instances. Although you get a few bits of functionality "for free" by
    116120      using Django models, ``items()`` can return any type of object you want.
     121    * ``item_description(self)`` is method that returns a ``Template`` object.
     122      This method of defining a ``Template`` can be used if you prefer this to
     123      creating template files located on the file system.
    117124    * If you're creating an Atom feed, rather than an RSS feed, set the
    118125      ``subtitle`` attribute instead of the ``description`` attribute. See
    119126      `Publishing Atom and RSS feeds in tandem`_, later, for an example.
     
    123130put into those elements.
    124131
    125132    * To specify the contents of ``<title>`` and ``<description>``, create
    126       `Django templates`_ called ``feeds/latest_title.html`` and
    127       ``feeds/latest_description.html``, where ``latest`` is the ``slug``
    128       specified in the URLconf for the given feed. Note the ``.html`` extension
    129       is required. The RSS system renders that template for each item, passing
    130       it two template context variables:
     133      `Django templates`_.  This can be done by either creating template files
     134      called ``feeds/latest_title.html`` and ``feeds/latest_description.html``,
     135      where ``latest`` is the ``slug`` specified in the URLconf for the given
     136      feed, or by defining the properties ``item_title`` and ``item_description``
     137      in your subclass.  Note the ``.html`` extension is required when using
     138      template files. The RSS system renders that template for each item,
     139      passing it two template context variables:
    131140
    132141          * ``{{ obj }}`` -- The current object (one of whichever objects you
    133142            returned in ``items()``).
     
    540549            django.core.exceptions.ObjectDoesNotExist on error.
    541550            """
    542551
     552        # ITEM TITLE -- If the template defined in title_template or the default
     553        # template file 'feeds/SLUG_title.html' does not exist, the framework
     554        # looks for this method.  If this method is not defined,
     555        # Template('{{ obj }}') is used by default.
     556
     557        def item_title(self):
     558            """
     559            Returns a Template object defining how to render the title.
     560            """
     561
    543562        # ITEM LINK -- One of these three is required. The framework looks for
    544563        # them in this order.
    545564
     
    556575            Returns the URL for every item in the feed.
    557576            """
    558577
     578        # ITEM DESCRIPTION -- If the template defined in description_template
     579        # or the default template file 'feeds/SLUG_description.html' does not
     580        # exist, the framework looks for this method.  If this method is not
     581        # defined, Template('{{ obj }}') is used by default.
     582
     583        def item_description(self):
     584            """
     585            Returns a Template object defining how to render the description.
     586            """
     587
    559588        # ITEM AUTHOR NAME --One of the following three is optional. The
    560589        # framework looks for them in this order.
    561590
Back to Top