python - Django的rss库生成的xml为什么浏览器不解析呢?
ringa_lee
ringa_lee 2017-04-18 10:12:07
0
1
525

URL配置:

from blog.feeds import RssSiteNewsFeed

urlpatterns = [
    url(r'^rss.xml$', RssSiteNewsFeed()),
]

feeds模块:

#!/use/bin/env python
# _*_ coding:utf-8 __

from django.contrib.syndication.views import Feed
from .models import Article
from django.utils.feedgenerator import Rss201rev2Feed


class CorrectMimeTypeFeed(Rss201rev2Feed):
    mime_type = 'application/xml'


class RssSiteNewsFeed(Feed):
    feed_type = CorrectMimeTypeFeed
    author_name = ""
    title = ""
    link = ""
    description = ""

    def items(self):
        return Article.objects.all().order_by('-created_time')[:5]

    def item_title(self, item):
        return item.title

    def item_link(self, item):
        return '/article/%s' % item.url

然后就出现了一个奇怪的现象,

明明已经解析出来了xml内容,有两个问题:

  1. 浏览器明明说了是Content-Type:application/rss+xml; charset=utf-8类型的文件,可是为什么显示的确实字符串呢?而不应该显示xml格式的内容。

ringa_lee
ringa_lee

ringa_lee

reply all(1)
阿神

Look at Django's documentation. By default, feed_type only supports three classes: RssUserland091Feed, Rss201rev2Feed, and Atom1Feed. Your code uses a custom class, which inherits from Rss201rev2Feed, so it needs to implement the root_attributes and add_root_elements methods. For details, you can view the custom Feed class

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template