article

Friday, May 22, 2020

Django how to create RSS feeds for the blog application


Django how to create RSS feeds for the blog application

RSS is an abbreviation for Really Simple Syndication, it’s a way to have information delivered to you instead of you having to go find it. RSS is basically a structured XML document that includes full or summarized text along with other metadata such as published date, author name, etc.

RSS is a web feed allows users and applications to access updates to websites in a standardized, computer-readable format.

In this article, we are going to generate feeds for a blog application

Part 1 : Django Build a blog application with bootstrap and Automatically Generate slugs

https://tutorial101.blogspot.com/2020/05/django-build-blog-application-with.html

Part 2 : Django Build a blog application Part 2 with WYSIWYG Editor Pagination and Comments

https://tutorial101.blogspot.com/2020/05/django-build-blog-application-part-2.html



 
#models.py
from django.db import models
 
from django.contrib.auth.models import User
from django.utils.text import slugify

STATUS = (
    (0,"Draft"),
    (1,"Publish")
)

class Post(models.Model):
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200, unique=True)
    author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='myapp_post')
    updated_on = models.DateTimeField(auto_now= True)
    content = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    status = models.IntegerField(choices=STATUS, default=0)
 
    class Meta:
        ordering = ['-created_on']

    def __str__(self):
        return self.title
  
    def get_absolute_url(self):
        from django.urls import reverse
        return reverse("post_detail", kwargs={"slug": str(self.slug)})
  
    class Meta:  
        db_table = "myapp_post"
  
    def save(self, *args, **kwargs):
        value = self.title
        self.slug = slugify(value, allow_unicode=True)
        super().save(*args, **kwargs)
Create a new file feeds.py inside the application directory.
 
#feeds.py
from django.contrib.syndication.views import Feed
from django.template.defaultfilters import truncatewords
from myapp.models import Post
from django.urls import reverse


class LatestPostsFeed(Feed):
    title = "My blog"
    link = ""
    description = "New posts of my blog."

    def items(self):
        return Post.objects.filter(status=1)

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

    def item_description(self, item):
        return truncatewords(item.content, 30)

    # Only needed if the model has no get_absolute_url method
    # def item_link(self, item):
    #     return reverse("post_detail", args=[item.slug])
The title, link, and description correspond to the standard RSS , <link> and <description> elements, respectively. <br/> map the feed in urls.py file.

from .feeds import LatestPostsFeed

urlpatterns = [
    path("feed/rss", LatestPostsFeed(), name="post_feed"),
               ]

RSS 2.0 feed at /feed/rss
navigate to http://127.0.0.1:8000/feed/rss you should see the XML file.

To explore more visit – https://docs.djangoproject.com/en/3.0/ref/contrib/syndication/

Related Post