Search code examples
pythonxmlwebapp2

Generating XML/Feed for your Python Blog


I've been trying to add RSS feeds in my blog(webapp2 application - Jinja2 templates), this is what I have:

class XMLHandler(Handler):
    def get(self):
        posts = db.GqlQuery("SELECT * FROM Post WHERE is_draft = FALSE ORDER BY created DESC")
        self.render("xmltemplate.xml", posts=posts, mimetype='application/xml')

xmltemplate.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <channel>
        <title>Blag</title>
        <link>http://www.blagonudacity.appspot.com/</link>
        <description>Yet Another Blag</description>
        {%for post in posts %}
        <entry>
            <title>{{post.subject}}></title>
            <link href="http://www.blagonudacity.appspot.com/post/{{ post.key().id()}}" rel="alternate" />
            <updated>{{post.created}}</updated>
            <author><name>Prakhar Srivastav</name></author>
            <summary type="html"> {{ post.content }} </summary>
        </entry>
        {%endfor%}
    </channel>
</feed>

What i'm getting in my browser when I migrate to the relevant page /feeds/all.atom.xml is just a html page with the markup. It doesn't look like how XML pages look in browser. What am I doing wrong here? Here is the demo


Solution

  • I saw that the page is delivered with content type text/html, this could be one problem, i suggest you should set this to text/xml (more details can be found here.

    Also it highly depends on the browser on how this is displayed, i guess you are using chrome (like me) where the link provided by you looks like a webpage, if you open it in firefox you will see the "live bookmark" styled page, however the entries don't show. I'm not sure if this is because of some problem with your markup or some problem with firefox and atom feeds.

    The xml file itself seems to be ok (checked with w3 validator).

    UPDATE: Ok, there seems to be something wrong with your atom XML (it is valid xml, as mentioned above) however it does not seem to be valid Atom data (according to the feed validator). I tried to bookmark it in firefox and it does not show any entries (just like the preview mentioned above). So i think you should take a look at the atom feed e.g. this and this could help.

    I'm not really sure but when looking at your XML i think that you may have mixed up Atom and Rss a little.