#gq: from django.contrib.auth import get_user_model from django.contrib.syndication.views import Feed # Django内置RSS/Atom订阅生成视图 from django.utils import timezone from django.utils.feedgenerator import Rss201rev2Feed # RSS 2.0标准生成器 from blog.models import Article # 博客文章模型 from djangoblog.utils import CommonMarkdown # Markdown解析工具,将Markdown转为HTML class DjangoBlogFeed(Feed): """博客RSS订阅Feed类,生成符合RSS 2.0标准的订阅内容""" feed_type = Rss201rev2Feed # 指定Feed类型为RSS 2.0 description = '大巧无工,重剑无锋.' # Feed描述 title = "且听风吟 大巧无工,重剑无锋. " # Feed标题(订阅列表中显示) link = "/feed/" # Feed的URL地址 def author_name(self): """Feed作者名称:取系统第一个用户的昵称""" return get_user_model().objects.first().nickname def author_link(self): """Feed作者链接:取系统第一个用户的个人主页URL""" return get_user_model().objects.first().get_absolute_url() def items(self): """Feed订阅的内容列表:最新5篇已发布(status='p')的文章(type='a')""" 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): """单个订阅项的链接:文章详情页URL""" return item.get_absolute_url() def item_guid(self, item): """单个订阅项的唯一标识(预留方法,暂未实现)""" return