Django provides a high-level aggregation content framework that lets us create rss/atom simple, and all you need to do is write a simple Python class.
First, examples
To create a feed, you just need to write a feed class and then set up a urlconf to the feed instance, which is very simple, here is an example that shows the last five news records for a site:
FromDjango.contrib.syndication.viewsImportFeedFromDjango.urlsImportReverseFromPolicebeat.modelsImportNewsItemClassLatestentriesfeed(Feed):Title="Police beat Site News"Link="/sitenews/"Description="Updates on changes and additions to police beat Central."DefItems(Self):ReturnNewsItem.Objects.Order_by('-pub_date ')[:5]DefItem_title(Self,Item):Returnitem. Title def item_description (self item): return item description # Item_link are only needed if newsitem have no Get_absolute_url method. def item_link (self): return reverse ( args=[item pk])
To set the URL of the link for this feed, simply add the instance of the feed class as a parameter, adding urlconf as follows:
URLlatestentriesfeedurl(r' ^latest/feed/$ 'latestentriesfeed# ... ]
Attention:
- The new Feed class inherits from the Django.contrib.syndication.views.Feed.
- The title, link, and description attributes correspond to standard RSS
<title>
, <link>
and elements, respectively <description>
.
- The items () method simply returns the list of objects that this feed needs to contain.
- If you want to create an atom feed instead of an RSS feed, use the subtitle property instead of description.
There is one more thing to do. In an RSS feed, each one <item>
has one <title>
, <link>
and <description>
we need to tell the framework what data to put into those objects.
For <title>
and <description>
, Django will attempt to invoke the and methods in the Feed class item_title()
item_description()
. Both methods are passed to a parameter: item, which is the object itself.
For <link>
, Django first tries to call the item_link()
method, and if the method does not exist, it uses the method defined in the object's ORM model get_absolute_url()
.
Ii. specify the type of feed
By default, using the RSS 2.0 type, if you want to specify a type, add the Feed_type attribute to the feed class as follows:
Atom1feedmyfeed(Feedatom1feed
There are three types available today:
- Django.utils.feedgenerator.Rss201rev2Feed (RSS 2.01. Default.)
- Django.utils.feedgenerator.RssUserland091Feed (RSS 0.91.)
- Django.utils.feedgenerator.Atom1Feed (Atom 1.0.)
Iii. simultaneous release of Atom and RSS feeds
To publish both, it's easy to create a subclass for your feed class, and set its feed_type to the type you want, and finally add a urlconf to it, as shown here:
FromDjango.contrib.syndication.viewsImportFeedFromPolicebeat.modelsImportNewsItemFromDjango.utils.feedgeneratorImportAtom1feedClassRsssitenewsfeed(Feed):Title="Police beat Site News"Link="/sitenews/"Description= "Updates on changes and additions to police beat Central." def items (selfreturn newsitem. Objects. Order_by ( '-pub_date ' ) [: 5]< Span class= "C1" ># Add the following subclass class atomsitenewsfeed ( Rsssitenewsfeed): feed_type = atom1feed # Modify type subtitle = rsssitenewsfeed . description
To increase the route:
URLrsssitenewsfeedatomsitenewsfeedurl(r' ^sitenews/rss/$ ' RsssitenewsfeedURL(r' ^sitenews/atom/$ 'atomsitenewsfeed# ... ]
Django-Aggregated content Rss/atom