首页 > 解决方案 > RSS 提要中没有更新日期

问题描述

Django 3.0.8

提要.py

class RssFeed(Feed):
    title = "Pcask.ru: все о компьютерах, гаджетах и программировании."
    link = get_site_address()
    description = "Новости, статьи, фотографии, видео о компьютерах и программировании."

    def items(self):
        # All evergreen posts and fresh news (just exclude old news).

        # Django taggit doesn't support excluding, only filtering. (https://django-taggit.readthedocs.io/en/latest/api.html#filtering)
        # Therefore we have to exclude items via an id list.
        excluded_items = Post.published.filter(tags__slug__in=[SPECIAL_TAGS.NEWS.value],
                                               updated__lte=timezone.now()-timedelta(days=NEWS_LIFESPAN))

        return Post.published.exclude(id__in=get_id_list(excluded_items))[:NUMBER_OF_ITEMS]


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

    def item_description(self, item):
        return item.description

    def item_categories(self, item):
        return (item.category.title,)

    feed_copyright = 'Все права защищены (c) {}, {}'.format(timezone.now().year,
                                                            get_site_address())

    def item_enclosure_url(self, item):
        featured_image = item.get_featured_image()
        img_field = getattr(featured_image, ENCLOSURE_WIDTH)
        enclosure_url = img_field.url
        return enclosure_url

    def item_enclosure_mime_type(self, item):
        featured_image = item.get_featured_image()
        img_field = getattr(featured_image, ENCLOSURE_WIDTH)
        _, ext = img_field.name.split(".")
        mime_type = get_mime_type(ext)
        return mime_type

    def item_enclosure_length(self, item):
        featured_image = item.get_featured_image()
        img_field = getattr(featured_image, ENCLOSURE_WIDTH)
        return img_field.size

    def item_pubdate(self, item):
        return item.created

    def item_updateddate(self, item):
        return item.updated

生成的提要(带有测试数据)

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Pcask.ru: все о компьютерах, гаджетах и программировании.</title>
        <link>http://localhost:8000</link>
        <description>Новости, статьи, фотографии, видео о компьютерах и программировании.</description>
        <atom:link href="http://localhost:8000/rss/" rel="self"></atom:link>
        <language>ru-RU</language>
        <copyright>Все права защищены (c) 2020, </copyright>
        <lastBuildDate>Mon, 13 Jul 2020 00:00:00 +0000</lastBuildDate>
        <item>
            <title>6ffd0cc4-c4cb-11ea-b89d-5404a66bf801</title>
            <link>http://localhost:8000/linux/6ffd0cc4-c4cb-11ea-b89d-5404a66bf801/</link>
            <description>1</description>
            <pubDate>Sun, 12 Jul 2020 00:00:00 +0000</pubDate>
            <guid>http://localhost:8000/linux/6ffd0cc4-c4cb-11ea-b89d-5404a66bf801/</guid>
            <enclosure length="15302" type="image/jpg" url="/media/raster_image/6w2RnwFGB2qv3LoQ9hafvf/350_1x-c63dTm9ByBxb8wYv2Tb6Md.jpg"></enclosure>
            <category>Linux</category>
        </item>
        <item>
            <title>6ffd0cc5-c4cb-11ea-b89d-5404a66bf801</title>
            <link>http://localhost:8000/linux/6ffd0cc5-c4cb-11ea-b89d-5404a66bf801/</link>
            <description>1</description>
            <pubDate>Sun, 12 Jul 2020 00:00:00 +0000</pubDate>
            <guid>http://localhost:8000/linux/6ffd0cc5-c4cb-11ea-b89d-5404a66bf801/</guid>
            <enclosure length="15302" type="image/jpg" url="/media/raster_image/6w2RnwFGB2qv3LoQ9hafvf/350_1x-c63dTm9ByBxb8wYv2Tb6Md.jpg"></enclosure>
            <category>Linux</category>
        </item>
...

问题

提要中没有更新日期。

文档中的示例包含更新: https ://docs.djangoproject.com/en/3.0/ref/contrib/syndication/#feed-class-reference

顺便说一句:如果我将 item_pubdate 或 item_pubdate 注释掉,xml 的架构不会改变。也许两者中的最新出现在提要中?RSS 2.0 规范不包含更新:https ://validator.w3.org/feed/docs/rss2.html

我该如何解决这个问题?

标签: django

解决方案


正如您所指出的,RSS 规范不包括updatedDate. 提要的默认提要类型是 RSS 2.01,它不会查找此参数。

如果您希望它出现在提要中,您需要使用不同的提要类型 - 例如Atom 1.0

如果我将 item_pubdate 注释掉,则 xml 的架构不会改变

我不明白为什么会这样,除非您在某处进行了一些缓存。


推荐阅读