Beautifying Django RssFeed

Hello @vitor, I’d like to know how you customized SIBTC’s feed.xml page to make it organized and beautiful. Thanks!

Hi @Cimmanuel

The blog front-end is a Jekyll website, so basically I have a template (similar to a Django template) to render the feed.xml.

Here is the snippet:

---
layout: null
---
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>{{ site.title | xml_escape }}</title>
    <description>{{ site.description | xml_escape }}</description>
    <link>{{ site.url }}{{ site.baseurl }}/</link>
    <atom:link href="{{ "/feed.xml" | prepend: site.baseurl | prepend: site.url }}" rel="self" type="application/rss+xml"/>
    <pubDate>{{ site.time | date_to_rfc822 }}</pubDate>
    <lastBuildDate>{{ site.time | date_to_rfc822 }}</lastBuildDate>
    <generator>Jekyll v{{ jekyll.version }}</generator>
    {% assign posts = site.posts | where_exp: "post", "post.hidden != true" %}
    {% for post in posts limit:10 %}
        <item>
            <title>{{ post.title | xml_escape }}</title>
            <description>{{ post.content | xml_escape }}</description>
            <pubDate>{{ post.date | date_to_rfc822 }}</pubDate>
            <link>{{ post.url | prepend: site.baseurl | prepend: site.url }}</link>
            <guid isPermaLink="true">{{ post.url | prepend: site.baseurl | prepend: site.url }}</guid>
            {% for tag in post.tags %}
            <category>{{ tag | xml_escape }}</category>
            {% endfor %}
            {% for cat in post.categories %}
            <category>{{ cat | xml_escape }}</category>
            {% endfor %}
        </item>
    {% endfor %}
  </channel>
</rss>
1 Like

Hello Vitor, since I’m not using Jekyll and I know the django Feed class has a default content-type of application/rss+xml; charset=utf-8, I simply modified the __call__ method in my own Feed (BlogFeed) class by doing:

      def __call__(self, request, *args, **kwargs):
            try:
                obj = self.get_object(request, *args, **kwargs)
            except ObjectDoesNotExist:
                raise Http404('Feed object does not exist.')
            feedgen = self.get_feed(obj, request)
            response = HttpResponse(content_type=feedgen.content_type) # ---> I removed this line...
            response = HttpResponse(content_type='application/xml; charset=utf-8') # ---> ...and added this
            if hasattr(self, 'item_pubdate') or hasattr(self, 'item_updateddate'):
                # if item_pubdate or item_updateddate is defined for the feed, set
                # header so as ConditionalGetMiddleware is able to send 304 NOT MODIFIED
                response['Last-Modified'] = http_date(
                    timegm(feedgen.latest_post_date().utctimetuple()))
            feedgen.write(response, 'utf-8')
            return response

I replaced line 7 with line 8 and I got a properly formatted Feed. This is doable, yeah? @vitor