Generating RSS Feeds With WebPy
2010-01-29 01:07:14 | 2 Comments
Some days ago I have seen someone asking how to generate RSS feeds with [webpy](http://www.mengu.net/tag/webpy). It is like generating feeds with any other web framework.
How the logic should be?
* Retrieve the items from the database.
* Create a RSS template.
* Set the content type.
* Print the items via template.
So, let's start with creating our feed class first.
class feed:
def GET(self):
date = datetime.today().strftime("%a, %d %b %Y %H:%M:%S +0200")
posts = db.select("post", order="id DESC", limit=10)
web.header('Content-Type', 'application/xml')
return render.feed(posts=posts, date=date)
This is all we need for generating for the feed. Now we can skip to create the feed template.
Mengu.net RSS Feed
http://www.mengu.net/
mengu on web programming.
en-us
{{ date }}
{{ date }}
Mengu.net
mengu@mengu.net
mengu@mengu.net
{% for post in posts %}
-
{{ post.title }}
http://www.mengu.net/post/{{ post.slug }}
{{ post.dateline }}
http://www.mengu.net/post/{{ post.dateline }}
{% endfor %}
This is a valid RSS feed. You can learn more on RSS feeds [here](http://cyber.law.harvard.edu/rss/rss.html). So, basically I have created a valid RSS template and then loop through the post items and I have my RSS working perfect.
Enjoy!
Check my latest project compector.com where you can post references about your former employers and see what others have said about the future employers. Sign up now and post a reference and share it on twitter or facebook.


Comments
Leave a Response