def get_before_article(self):#返回当前文章的前一篇文章
temp = Article.objects.order_by('id')
cur = Article.objects.get(id=self.id)
count=0
for i in temp:
if i.id == cur.id:
index = count
break
else:
count=count+1
if index != 0:
return temp[index-1]
这个方法 中的 count=count+1 应该是写错了吧, 应该是index=1
而且这应该不用写for循环;
temp = Article.objects.values_list('id', flat=True).order_by('id')
cur = Article.objects.get(id=self.id)
if cur in temp:
index = temp.index(cur)
else:
index=1
return temp[index-1]