首页 > micolog > 为micolog增加随机文章功能

为micolog增加随机文章功能

2009年8月22日 发表评论 阅读评论

edikud提到希望增加随机文章的功能,其实这个与增加热门文章类似,也是修改model.py文件,Blog类,就在上次的hotposts函数下面,增加下面一段代码(那个num=3是说每次取3个随机文章)

def randposts(self, num = 5):
entries = Entry.all().filter('entrytype =', 'post').filter('published =', True)
#TODO use entry_num since already got the entries from db
if not entries or num >= g_blog.entrycount:
return entries
rand_ids = {}
for i in range(0, num):
rand = random.randint(0, g_blog.entrycount - 1)
#hope the entrycount in the blog is big a lot then num
while rand_ids.has_key(rand):
rand = random.randint(0, g_blog.entrycount - 1)
rand_ids.update({rand:True})

result = []
for id in rand_ids.keys():
result.append(entries.fetch(1, offset = id)[0])

return result

加好后,就修改模板文件sidebar.html,拷贝原来的热门文章的那一段就可以啦,只需要把其中的hotposts修改为randposts

---------------------------------------------------------------
本站作品根据创作共同协议进行授权, 转载时请务必以超链接形式标明文章原始出处
原文地址:http://www.mirecle.com/2009/08/22/to-increase-the-random-article-feature-micolog.html
---------------------------------------------------------------

您可能会喜欢:

  1. 为micolog增加热门文章功能
  2. 升级并小修正micolog相关文章功能
  3. 为micolog增加阅读次数统计
  4. 修正micolog页码输出从零开始的问题
  5. 再次升级micolg的留言验证,防止垃圾评论
分类: micolog 标签:

  1. 2009年8月22日18:32 | #1

    果然是大“湿”,很是感谢!

  2. 2009年8月22日18:34 | #2

    下次回来就加上…

  3. 2009年8月25日09:18 | #3

    不错呀 小窝弄的挺好

  4. 2009年8月25日09:29 | #4

    嘿嘿,还凑合吧,就是时间太少,只能零零散散的敲敲打打,等blog程序比较满足需求了就开始整别的了

  5. 2009年8月26日07:06 | #5

    yanpeng 整的这几个功能非常好。我最近再等徐明的新版,我的源码让我改的有问题了。所以最近的界面上更新都没上传上来。过段时间 你这些都加进来

  6. 2009年9月4日13:59 | #6

    挺好的~呵呵 看到的有点晚

  7. 2009年9月24日08:57 | #7

    def randposts(self, num = 8):

    entries = Entry.all().filter(‘entrytype =’, ‘post’).filter(‘published =’, True)

    #TODO use entry_num since already got the entries from db

    if not entries or num >= g_blog.entrycount:

    return entries

    rand_ids = {}

    for i in range(0, num):

    rand = random.randint(0, g_blog.entrycount – 1)

    #hope the entrycount in the blog is big a lot then num

    while rand_ids.has_key(rand):

    rand = random.randint(0, g_blog.entrycount – 1)

    rand_ids.update({rand:True})

    result = []

    for id in rand_ids.keys():

    result.append(entries.fetch(1, offset = id)[0])

    return result

  8. 2009年9月24日08:57 | #8

    删了上面的吧 本想贴个有缩进的 看看对不对

  9. 2009年9月24日08:59 | #9

        def randposts(self, num = 8): 

            entries = Entry.all().filter(‘entrytype =’, ’post’).filter(‘published =’, True) 

            #TODO use entry_num since already got the entries from db 

            if not entries or num >= g_blog.entrycount: 

                return entries 

            rand_ids = {} 

            for i in range(0, num): 

                rand = random.randint(0, g_blog.entrycount - 1) 

            #hope the entrycount in the blog is big a lot then num 

            while rand_ids.has_key(rand): 

                rand = random.randint(0, g_blog.entrycount - 1) 

            rand_ids.update({rand:True})

            result = [] 

            for id in rand_ids.keys(): 

                result.append(entries.fetch(1, offset = id)[0])

            return result

  10. 2009年9月24日09:35 | #10

    while rand_ids.has_key(rand):那一行是与上面的rand那里对齐,rand_ids那一行是与while对齐

    囧啊,看来需要装个显示代码的插件才行了

  11. 2009年9月24日09:56 | #11

    我是全部用空格,错误提示和没提示一样“A server error occurred. Please contact the administrator.”

    我修改了下 entries

    然后返回 entries 是可以的。但就是中间那块代码不行!!

  12. 2009年9月24日10:06 | #12

    本来代码 是64行的 rand往前再进两格 刚改了试试 抓了个图

  13. 2009年9月24日10:08 | #13

    谢谢 下载了 回来再看

  14. 2009年9月24日11:45 | #14

    知道原因所在了 没有导入random库

  15. 2009年9月24日16:50 | #15

    晕倒,是因为随机文章的问题么。这种情况下,首先需要仔细想一下最近做的修改。比如做的是增加了新功能,类似于随机文章这种简单的,就可以在模板上先不调用这个函数。另外,我发现那个entry.publish的函数我给改错了,还是不要改那个了吧,因为底下还有个if判断,g_blog的entryCount是在那里增加的

  16. 2009年9月24日08:58 | #16

    def randposts(self, num = 8):

    entries = Entry.all().filter(‘entrytype =’, ‘post’).filter(‘published =’, True)

    #TODO use entry_num since already got the entries from db

    if not entries or num >= g_blog.entrycount:

    return entries

    rand_ids = {}

    for i in range(0, num):

    rand = random.randint(0, g_blog.entrycount – 1)

    #hope the entrycount in the blog is big a lot then num

    while rand_ids.has_key(rand):

    rand = random.randint(0, g_blog.entrycount – 1)

    rand_ids.update({rand:True})

    result = []

    for id in rand_ids.keys():

    result.append(entries.fetch(1, offset = id)[0])

    return result

  17. 2009年9月24日08:59 | #17

    请问缩进是上面那样吗?

  18. 2009年9月24日09:45 | #18

    按这样还是不行 你复制下面的代码到记事本看看 还有什么错

        def randposts(self, num = 8): 

            entries = Entry.all().filter(‘entrytype =’, ’post’).filter(‘published =’, True) 

            #TODO use entry_num since already got the entries from db 

            if not entries or num >= g_blog.entrycount: 

                return entries 

            rand_ids = {} 

            for i in range(0, num): 

                rand = random.randint(0, g_blog.entrycount - 1) 

                #hope the entrycount in the blog is big a lot then num 

                while rand_ids.has_key(rand): 

                    rand = random.randint(0, g_blog.entrycount - 1) 

                rand_ids.update({rand:True})

            result = [] 

            for id in rand_ids.keys(): 

                result.append(entries.fetch(1, offset = id)[0])

            return result

  19. 2009年9月24日09:50 | #19

    这个缩进情况和我代码里面是一样的。是报缩进的语法错误么,缩进的时候最好用空格而不是tab。用vim或者eclipse把缩进都处理一下可能就会好了

  20. 2009年9月24日10:00 | #20

    你先用我的这个model.py文件试试?我晚上回家升级一把micolog解决空格和语法高亮的问题,记得有篇文章讲过这个事情来着

    http://dl.getdropbox.com/u/1002712/model.py

  21. 2009年9月24日10:04 | #21

    我先吃饭去了 回来再细细研究! 看图 应该没什么错!

  22. 2009年9月24日12:55 | #22

    囧rz

  23. 2009年9月24日14:09 | #23

    似乎 还是有问题 我在本地测试没问题 上传上去就会有打不开的情况 有时要刷新三次才会打开

    有时打开一些新的文章时又会出现打不开的情况 是不是我文章多 什么原因 还要等等吗? 我在后台用版本的链接做测试 所以把缓存都关了!

  24. 2009年9月24日14:15 | #24

    其实本地开始测试 也是经常打不开 大多需要刷新三次 后来我清空了所有文章 并安装了 最新的gae 重启电脑后 还是有点问题 首页无法显示。后来选中仅有的几篇文章删除重新添加几篇就ok了。

    难道和数据有关系吗?

  25. 2009年9月24日15:10 | #25

    同一个程序 传了 不同文章数的 就不同

    http://hikeblog.appspot.com/

    http://171.latest.hou-kai.appspot.com/ 首页有时能打开 单页几乎都打不开 其中一页刷了几次才出来

    我是什么原因呢

  26. 2009年9月25日04:13 | #26

    我现在改回了只做了 随机文章这个功能。发现改 num 不会有变动

    比如 我有5篇日志 ,改num为2或3 但显示出来的还是5篇随机文章

    要不我把我的源码发给你 你测试一下!

  27. 2010年1月15日06:57 | #27

    viagra

    http://www.getviagrarx.com/

    [url=http://www.getviagrarx.com/]viagra[/url]

  28. ghs
    2010年2月1日16:21 | #28

    你好,我用了你的代码,但是一直都不能成功实现随机文章功能,你能不能发一份你修改过的micolog程序给我。或者把修改过的model.py和index.yaml这两个文件发给我下.谢谢了.

  29. 2010年2月1日16:32 | #29

    请到http://dl.dropbox.com/u/1002712/micolog.rar下载我修改该的那个版本。
    要想出现随机文章,是需要皮肤支持的,下载安装后,请使用附带的inove主题查看效果,另外我那个因为有一些饭spam的东东也是需要附带的inove主题配合的,要不然留言功能会被囧到

  1. 本文目前尚无任何 trackbacks 和 pingbacks.