You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Django/doc/djangoblog/feeds.py

58 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from django.contrib.auth import get_user_model
from django.contrib.syndication.views import Feed
from django.utils import timezone
from django.utils.feedgenerator import Rss201rev2Feed
# 导入自定义模型和工具
from blog.models import Article
from djangoblog.utils import CommonMarkdown
class DjangoBlogFeed(Feed):
"""DjangoBlog的RSS订阅源类"""
# 指定Feed类型为RSS 2.0
feed_type = Rss201rev2Feed
# Feed的描述信息
description = '大巧无工,重剑无锋.'
# Feed的标题
title = "且听风吟 大巧无工,重剑无锋. "
# Feed的链接地址
link = "/feed/"
def author_name(self):
"""获取作者名称 - 返回第一个用户的昵称"""
return get_user_model().objects.first().nickname
def author_link(self):
"""获取作者链接 - 返回第一个用户的绝对URL"""
return get_user_model().objects.first().get_absolute_url()
def items(self):
"""获取要在Feed中显示的项目列表"""
# 返回最近发布的5篇文章过滤条件类型为'article'且状态为'published'
return Article.objects.filter(type='a', status='p').order_by('-pub_time')[:5]
def item_title(self, item):
"""获取单个项目的标题"""
return item.title
def item_description(self, item):
"""获取单个项目的描述 - 将Markdown内容转换为HTML"""
return CommonMarkdown.get_markdown(item.body)
def feed_copyright(self):
"""获取Feed的版权信息"""
now = timezone.now()
return "Copyright© {year} 且听风吟".format(year=now.year)
def item_link(self, item):
"""获取单个项目的链接"""
return item.get_absolute_url()
def item_guid(self, item):
"""获取单个项目的全局唯一标识符(当前未实现)"""
# 注意:这个方法目前没有返回值,可能需要根据需求实现
# 通常应该返回一个唯一标识项目的字符串如文章的ID或永久链接
pass