Django聚合,如何替换根元素

u1ehiz5o  于 2023-06-25  发布在  Go
关注(0)|答案(1)|浏览(93)

假设我使用django内置的聚合框架生成rss页面。

<channel xmlns:content="http://purl.org/rss/1.0/modules/content/">
<title>laike9m's blog</title>
<link>http://example.com/blog/rss</link>
<description>Update on laike9m blog's articles.</description>
<atom:link rel="self" href="http://example.com/blog/rss/"/>
<language>en-us</language>
...

我想改变<language>标签的内容,该怎么做?
我尝试定义自定义提要:

class ExtendedRssFeed(Rss201rev2Feed):

    def add_root_elements(self, handler):
        super(ExtendedRssFeed, self).add_root_elements(handler)
        handler.addQuickElement('language', 'zh-CN')

它不会替换,而是添加另一个<language>标记。

<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel xmlns:content="http://purl.org/rss/1.0/modules/content/">
<title>laike9m's blog</title>
<link>http://example.com/blog/rss</link>
<description>Update on laike9m blog's articles.</description>
<atom:link rel="self" href="http://example.com/blog/rss/"/>
<language>en-us</language>
<language>zh-CN</language>
insrf1ej

insrf1ej1#

通过为我的Feed覆盖__init__来做到这一点,如下所示:

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

class ItunesFeed(Rss201rev2Feed):

    def __init__(self, title, link, description, language=None, *args, **kwargs):
        return super().__init__(title, link, description, language='zh-CN', *args, **kwargs)

class PodcastFeed(Feed):
    feed_type = ItunesFeed

相关问题