给micolog增加最热最冷文章排行及评论排行

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)

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>