1.打开model.py,在class Blog(db.Model):最后添加如下代码(红色部分)数字代表显示列表数目
|
def recentposts(self): return Entry.all().filter(‘entrytype =’,'post’).order(‘-date’).fetch(8)
def hotposts(self): return Entry.all().filter(‘entrytype =’,'post’).filter("published =", True).order(‘-readtimes’).fetch(8)
def coldposts(self): return Entry.all().filter(‘entrytype =’,'post’).order(‘readtimes’).fetch(8)
|
2.在sidebar.html中添加 我的如下:
|
<div class="block"> <h2>热门文章</h2> <ul> {% for entry in blog.hotposts %} <li><a href="/{{entry.link}}">{{entry.title}}</a> ({{entry.readtimes}})</li> {%endfor%} </ul> </div>
<div class="block"> <h2>冷门文章</h2> <ul> {% for entry in blog.coldposts %} <li><a href="/{{entry.link}}">{{entry.title}}</a> ({{entry.readtimes}})</li> {%endfor%} </ul> </div>
|
在本地测试成功再上传到GAE!
另外,如果要添加热门评论可以在步骤1代码后面加入如下代码
|
def hotcommentposts(self): return Entry.all().filter(‘entrytype =’,'post’).order(‘-commentcount’).fetch(8)
|