给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)

本文永久链接  http://www.houkai.com/2009/07/7/micolog-hotposts.html
转载注明来自  HOUKAI.COM 分享家:Addthis中国

用windows live writer发布Micolog日志»

«给micolog增加google自定义搜索(站内搜索)




最新文章

您可能会喜欢

本文共有17评论

  1. 你看我的首页用IE访问进入分类,字体变大时怎么回事啊?还有那个顶部的菜单,鼠标效果为何是一半黑的一半白的。用GOOGLE浏览器访问就没有这问题。

  2. 还以为你的是wp呢。。。
    看来micolog也不错

  3. 用了你的model.py也不行?

  4. @edikud
    你要的是什么功能,如果是本文的功能,还需在主题文件里修改

  5. 那还要加上什么啊!我试了几次了都不行!

  6. 那还要加上什么啊!我试了几次了都不行!【引用 edikud】
    ——————————————————————
    哦 忘了你要什么功能了 是本文的所写的那个功能吗?
    如果你用了我的model文件,再看文章的第2步

  7. @houkai
    是本文的所写的那个功能....

  8. 文章的第2步,我加上就是不行?没有加就没事。。

  9. 文章的第2步,我加上就是不行?没有加就没事。。 【引用 edikud】
    ——————————————————————
    索引有问题 删除索引重新生成以下 试试

  10. 。。。。
    。。。。
    。。。。
    File "/base/data/home/apps/madesignsky/1.335420644634200455/model.py", line 146, in hotposts
    return Entry.all().filter('entrytype =','post').order('-readtimes').fetch(5)
    File "/base/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 1455, in fetch
    raw = self._get_query().Get(limit, offset)
    File "/base/python_lib/versions/1/google/appengine/api/datastore.py", line 981, in Get
    prefetch_count=limit)._Get(limit)
    File "/base/python_lib/versions/1/google/appengine/api/datastore.py", line 929, in _Run
    str(exc) + '\nThis query needs this index:\n' + yaml)
    NeedIndexError: no matching index found.
    This query needs this index:
    - kind: Entry
    properties:
    - name: entrytype
    - name: readtimes
    direction: desc




    文章的第2步,我加上上传出现上面的错误。 删除索引重新生成也不行。。。

  11. 不能添加这个,不然就会出现服务器错误,请联系管理员。

  12. @iandmmo
    注意代码的缩进。另外你可以删除索引。或者用下面的索引试试
    -------------------------------------------
    indexes:

    # AUTOGENERATED

    # This index.yaml is automatically updated whenever the dev_appserver
    # detects that a new type of query is run. If you want to manage the
    # index.yaml file manually, remove the above marker line (the line
    # saying "# AUTOGENERATED"). If you want to manage some indexes
    # manually, move them above the marker line. The index.yaml file is
    # automatically uploaded to the admin console when you next deploy
    # your application using appcfg.py.

    # Used 930 times in query history.
    - kind: Comment
    properties:
    - name: entry
    - name: date

    # Unused in query history -- copied from input.
    - kind: Entry
    properties:
    - name: categorie_keys
    - name: date
    direction: desc

    # Used 301 times in query history.
    - kind: Entry
    properties:
    - name: entry_parent
    - name: entrytype
    - name: published
    - name: menu_order

    # Used 294 times in query history.
    - kind: Entry
    properties:
    - name: entrytype
    - name: commentcount
    direction: desc

    # Used 307 times in query history.
    - kind: Entry
    properties:
    - name: entrytype
    - name: date
    direction: desc

    # Used 123 times in query history.
    - kind: Entry
    properties:
    - name: entrytype
    - name: post_id

    # Used 123 times in query history.
    - kind: Entry
    properties:
    - name: entrytype
    - name: post_id
    direction: desc

    # Used 51 times in query history.
    - kind: Entry
    properties:
    - name: entrytype
    - name: published
    - name: date
    direction: desc

    # Used 2 times in query history.
    - kind: Entry
    properties:
    - name: entrytype
    - name: published
    - name: slug
    - name: date

    # Used 123 times in query history.
    - kind: Entry
    properties:
    - name: entrytype
    - name: readtimes

    # Used 297 times in query history.
    - kind: Entry
    properties:
    - name: entrytype
    - name: readtimes
    direction: desc

    # Unused in query history -- copied from input.
    - kind: Entry
    properties:
    - name: published
    - name: date
    direction: desc

    # Unused in query history -- copied from input.
    - kind: Entry
    properties:
    - name: tags
    - name: date
    direction: desc

    # Used 126 times in query history.
    - kind: Entry
    properties:
    - name: tags
    - name: post_id
    direction: desc

  13. @houkai 还是不行啊,如果可能的话,能提供你使用的网站源码吗。
    好像只要第一步出现代码里面出现readtimes后,调用就会出错。

  14. @iandmmo
    知道了 你要先给Micolog添加阅读次数 见www.houkai.com/?p=5003

  15. 我改了以后提示缩进错误,真麻烦呀。

  16. 感谢HOUKAI,第二次测试成功。开启Notepad++视图里的“显示空格和制表符”真的很有用!

 

现在评论本文

4+3