为micolog增加随机文章功能
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
---------------------------------------------------------------
您可能会喜欢:
分类: micolog
果然是大“湿”,很是感谢!
下次回来就加上…
不错呀 小窝弄的挺好
嘿嘿,还凑合吧,就是时间太少,只能零零散散的敲敲打打,等blog程序比较满足需求了就开始整别的了
yanpeng 整的这几个功能非常好。我最近再等徐明的新版,我的源码让我改的有问题了。所以最近的界面上更新都没上传上来。过段时间 你这些都加进来
挺好的~呵呵
看到的有点晚 
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
删了上面的吧 本想贴个有缩进的 看看对不对
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
while rand_ids.has_key(rand):那一行是与上面的rand那里对齐,rand_ids那一行是与while对齐
囧啊,看来需要装个显示代码的插件才行了
我是全部用空格,错误提示和没提示一样“A server error occurred. Please contact the administrator.”
我修改了下 entries
然后返回 entries 是可以的。但就是中间那块代码不行!!
本来代码 是64行的 rand往前再进两格 刚改了试试 抓了个图
谢谢 下载了 回来再看
知道原因所在了 没有导入random库
晕倒,是因为随机文章的问题么。这种情况下,首先需要仔细想一下最近做的修改。比如做的是增加了新功能,类似于随机文章这种简单的,就可以在模板上先不调用这个函数。另外,我发现那个entry.publish的函数我给改错了,还是不要改那个了吧,因为底下还有个if判断,g_blog的entryCount是在那里增加的
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
请问缩进是上面那样吗?
按这样还是不行 你复制下面的代码到记事本看看 还有什么错
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
这个缩进情况和我代码里面是一样的。是报缩进的语法错误么,缩进的时候最好用空格而不是tab。用vim或者eclipse把缩进都处理一下可能就会好了
你先用我的这个model.py文件试试?我晚上回家升级一把micolog解决空格和语法高亮的问题,记得有篇文章讲过这个事情来着
http://dl.getdropbox.com/u/1002712/model.py
我先吃饭去了 回来再细细研究! 看图 应该没什么错!

囧rz
似乎 还是有问题 我在本地测试没问题 上传上去就会有打不开的情况 有时要刷新三次才会打开
有时打开一些新的文章时又会出现打不开的情况 是不是我文章多 什么原因 还要等等吗? 我在后台用版本的链接做测试 所以把缓存都关了!
其实本地开始测试 也是经常打不开 大多需要刷新三次 后来我清空了所有文章 并安装了 最新的gae 重启电脑后 还是有点问题 首页无法显示。后来选中仅有的几篇文章删除重新添加几篇就ok了。
难道和数据有关系吗?
同一个程序 传了 不同文章数的 就不同
http://hikeblog.appspot.com/
http://171.latest.hou-kai.appspot.com/ 首页有时能打开 单页几乎都打不开 其中一页刷了几次才出来
我是什么原因呢
我现在改回了只做了 随机文章这个功能。发现改 num 不会有变动

比如 我有5篇日志 ,改num为2或3 但显示出来的还是5篇随机文章
要不我把我的源码发给你 你测试一下!
viagra
http://www.getviagrarx.com/
[url=http://www.getviagrarx.com/]viagra[/url]
你好,我用了你的代码,但是一直都不能成功实现随机文章功能,你能不能发一份你修改过的micolog程序给我。或者把修改过的model.py和index.yaml这两个文件发给我下.谢谢了.
请到http://dl.dropbox.com/u/1002712/micolog.rar下载我修改该的那个版本。
要想出现随机文章,是需要皮肤支持的,下载安装后,请使用附带的inove主题查看效果,另外我那个因为有一些饭spam的东东也是需要附带的inove主题配合的,要不然留言功能会被囧到