Tech Monger

Programming, Web Development and Computer Science.

Skip to main content| Skip to information by topic

Atom RSS Feed Generator with Python and Flask

If you have custom content management system written in python with direct control on your code then you can publish your blog feeds without using feed services like feedburner. With custom code you will also be able to generate feeds for specific tags on your blog. In this post we will generate feeds using python module called werkzeug.

Before We Begin..

We will use werkzeug's AtomFeed class to render Atom feed. Make sure you can fetch following values from your blog database.

Generate Feed Object

AtomFeed constructor would take following value parameters to generate feed object.

Value Description
feed_url URL Route of Published Feed.
Example :
url Root URL of Blog.
Example :
feed = AtomFeed(title='Last 10 Posts from My Blog',

Appending Blog Entries in Feed Object

Once feed object is created we would call feed.add() method and provide following blog entry details as parameter to it.

Value Description
title Post Title
summary Blog post summary.
content_type html
Example :
author Name of author.
url Absolute URL for the blog post.
updated Last updated date and time in UTC format.
published Created date and time in UTC format.
for post in Post.query.limit(10).all():
        feed.add(post.title, post.summary, content_type='html',
       , url=post.url, updated=post.last_mod_date,

Implementation in Flask

from Flask import request
from urlparse import urljoin
from werkzeug.contrib.atom import AtomFeed

def get_abs_url(url):
    """ Returns absolute url by joining post url with base url """
    return urljoin(request.url_root, url)

def feeds():
    feed = AtomFeed(title='Last 10 Posts from My Blog',
                    feed_url=request.url, url=request.url_root)

    # Sort post by created date
    posts = get_last_10_posts()

    for post in posts:
        feed.add(post.title, post.content,
                 author= post.author_name,

    return feed.get_response()

To customize feed further conforming with atom specification refer to the werkzeug documentation.

Tagged Under : Flask Python Web