Adding an RSS feed to your Django project is a rather simple task, that being said it is a feature which is highly extensible.
The following tutorial will outline a simple implementation.
Say, you have a blog app within your project which you want to add an RSS feed to, the first thing you will want to do is to create a `feeds.py` file within the apps directory.
.
├── admin.py
├── apps.py
├── feeds.py
├── __init__.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
├── urls.py
└── views.py
Next, you will want to construct a `LatestPostsFeed()` class within `feeds.py`. This class represents all of the published blog articles which will in turn be used to render all of the individual RSS feeds which represent the published articles.
from django.contrib.syndication.views import Feed from django.template.defaultfilters import truncatewords from django.urls import reverse from django.utils.html import strip_tags from .models import Blog class LatestPostsFeed(Feed): title = "A person's RSS Feed" link = "" description = "New blog posts..." def items(self): return Blog.objects.published() def item_title(self, item): return item.title def item_description(self, item): plain = strip_tags(item.blog) return truncatewords(plain, 30) def item_link(self, item): return reverse("blog:detail", args=[item.slug])
Next, the `urlpatterns` in `urls.py` will need to be updated to include an entry for the RSS feeds.
from django.urls import path from blog import views from blog.feeds import LatestPostsFeed app_name = 'blog' urlpatterns = [ ..., path('feed/', LatestPostsFeed()), ..., ]
You should now be able to visit url `127.0.0.1:8000/blog/feed/` and view your RSS feeds.
At this point you will notice that the `example.com` will be present in the url string. To update this go to the `Sites` config in the admin panel, and update the current entry, or delete the current entry and create a new one.
Finally, include a link to your RSS feed in your header or footer templates to allow RSS enabled browsers to see the link and allow readers to subscribe to your feed automatically.