Example of Django adding feeds functionality

  • 2020-12-07 04:05:42
  • OfStack

Concept: Both RSS and Atom are based on the XML format, which you can use to provide feed with automated updates about your site's content. Learn more about RSS can access http: / / www whatisrss. com /, more information can be accessed Atom http: / / www atomenabled. org /.

RSS (Simple Aggregation of Information) is a source format specification for aggregating frequently updated data from web sites, such as blog posts, news, audio, or video extracts. RSS files (or abstracts, network abstracts, or frequent updates, provided to channels) contain the full text or excerpts, plus the extracted data and authorized metadata to which the publisher subscribed.

So you can subscribe to your favorite websites using tools like feedly, so that when their websites are updated, you can read the updates through feedly instead of going to the site to check them out.

Here's how to add RSS to your Django site. It's actually quite simple:

1. First, an Feed class is established. The Feed class provides the data required by the source :title,link,description

Example code is as follows: Create ES36en.py:


from django.contrib.syndication.views import Feed
from blog.models import Article
from django.conf import settings
from django.utils.feedgenerator import Rss201rev2Feed
from DjangoBlog.common_markdown import common_markdown
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.contrib.sites.models import Site


class DjangoBlogFeed(Feed):
  feed_type = Rss201rev2Feed

  description = settings.SITE_DESCRIPTION
  feed_url = 'https://www.fkomm.cn/feed'
  title = "%s %s " % (settings.SITE_NAME, settings.SITE_DESCRIPTION)
  link = "https://www.fkomm.cn"

  def author_name(self):
    return get_user_model().objects.first().nickname

  def author_link(self):
    return get_user_model().objects.first().get_absolute_url()

  def items(self):
    return Article.objects.order_by('-pk')[:5]

  def item_title(self, item):
    return item.title

  def item_description(self, item):
    return common_markdown.get_markdown(item.body)

  def feed_copyright(self):
    # print(Site.objects.get_current().name)
    return "Copyright© 2018 " + settings.SITE_NAME

  def item_link(self, item):
    return item.get_absolute_url()

  def item_guid(self, item):
    return

2. Then add to ES41en. py:


from DjangoBlog.feeds import DjangoBlogFeed


urlpatterns = [
  ......
  url(r'^feed/$',DjangoBlogFeed()),
]

At this point, it's all done and ready to open


Related articles: