We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Life is a maze and love is a riddle.
Creating feeds for your blog posts
blog
feeds.py
from django.contrib.syndication.views import Feed from django.template.defaultfilters import truncatewords from .models import Post class LatestPostsFeed(Feed): title = 'My blog' link = '/blog/' description = 'New posts of my blog.' def items(self): return Post.published.all()[:5] def item_title(self, item): return item.title def item_description(self, item): return truncatewords(item.body, 30)
title
link
description
RSS
<title>
<link>
<description>
items()
feed
posts
item_title()
item_description()
truncatewords()
blog/urls.py
LatestPostsFeed
URL
from .feeds import LatestPostsFeed urlpatterns = [ #... url(r'^feed/$', LatestPostsFeed(), name='post_feed'), ]
http://localhost:8000/blog/feed/
RSS feed
blog/base.html
total posts
<p><a href="{% url "blog:post_feed" %}">Subscribe to my RSS feed</a></p>
The text was updated successfully, but these errors were encountered:
No branches or pull requests
0x01 Django
Creating feeds for your blog posts
blog
文件夹下创建feeds.py
title
link
description
3个属性分别对应RSS
中的<title>
<link>
<description>
元素items()
函数为feed
选取5个公开posts
item_title()
函数 和item_description()
函数返回了选取的posts
对应的title
description
(这里用truncatewords()
截取了30个单词)feeds.py
后,在blog/urls.py
中导入刚刚创建的LatestPostsFeed
,匹配一个新的URL
http://localhost:8000/blog/feed/
就可以看到RSS feed
(包括了5个posts
)blog/base.html
,在total posts
下添加The text was updated successfully, but these errors were encountered: