您的位置:首页 > Web前端

django自身提供的sitemap和feed实现样例

2016-07-07 14:26 489 查看
《DJANGO BY EXAMPLE》这书的例子真是精心全过的,

基本的WEB开发过程全覆盖啊。

跟着一步一步的弄就OK啦。。可以长很多知道的。

这次跟着作的是sitemap和feed功能。

sitemap.py

from django.contrib.sitemaps import Sitemap
from .models import Post

class PostSitemap(Sitemap):
changefreq = 'weekly'
priority = 0.9

def items(self):
return Post.published.all()

def lastmod(self, obj):
return obj.publish


feeds.py

from django.contrib.syndication.views import Feed
from django.template.defaultfilters import truncatewords
from .models import Post

class LatestPostFeed(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)


urls.py

url(r'^sitemap\.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),

url(r'^feed/$', LatestPostFeed(), name='post_feed'),


样子:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: