RSS/Atom Feeds with Syndication Framework

A snippet showing how to create RSS/Atom feeds.


# models.py
from django.db import models
class News(models.Model):
    title = models.CharField(max_length=120)
    slug = models.SlugField(unique=True)
    created_at = models.DateTimeField(auto_now_add=True)
  

# admin.py

from django.contrib import admin
from .models import News
@admin.register(News)
class NewsAdmin(admin.ModelAdmin):
    list_display = ("title","created_at")
  

  

# feeds.py
from django.contrib.syndication.views import Feed
from .models import News
class LatestNewsFeed(Feed):
    title = "Latest News"
    link = "/news/"
    description = "Updates on the latest news."
    def items(self): return News.objects.order_by("-created_at")[:20]
    def item_link(self, item): return f"/news/{item.slug}/"

# urls.py
from .feeds import LatestNewsFeed
path("feeds/latest/", LatestNewsFeed(), name="news-feed")
  
Explanation:
  • Use django.contrib.syndication feeds to publish RSS/Atom easily.
  • Implement items() and link methods for entries.
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.