我一直在尝试在我的博客中添加 RSS 提要(webapp2 应用程序 - Jinja2 模板),这就是我所拥有的:
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 看起来像这样:
<?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>
当我迁移到相关页面 /feeds/all.atom.xml
时,我在浏览器中得到了什么
只是一个带有标记的 html 页面。它看起来不像 XML 页面在浏览器中的样子。我在这里做错了什么? Here is the demo
最佳答案
我看到页面以内容类型 text/html 交付,这可能是一个问题,我建议您将其设置为 text/xml (更多详细信息可以在 here 中找到。
此外,它的显示方式在很大程度上取决于浏览器,我猜你使用的是 chrome(就像我一样),你提供的链接看起来像一个网页,如果你在 firefox 中打开它,你会看到“实时书签” "样式页面,但条目不显示。我不确定这是因为您的标记有问题还是 firefox 和 atom feeds 有问题。
xml 文件本身似乎没问题(用 w3 validator 检查)。
更新:好的,您的 Atom XML 似乎有问题(它是有效的 XML,如上所述)但是它似乎不是有效的 Atom 数据(根据 feed validator ) . 我试图在 firefox 中将它添加为书签,但它没有显示任何条目(就像上面提到的预览一样)。 所以我认为你应该看看原子饲料,例如this 和 this 可以提供帮助。
我不太确定,但在查看您的 XML 时,我认为您可能有点混淆了 Atom 和 Rss。
关于python - 为你的 Python 博客生成 XML/Feed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10460070/