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.
DjangoBlog/djangoblog/feeds.py

49 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.

#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