这里主要说Django框架的forloop函数
我这里有个例子,需要的直接调用。我暂时还没时间研究这个forloop。
def index(request):
comment_obj = Comment.objects.all()
currentPage = int(request.GET.get('page', 1))
paginator = Paginator(comment_obj, 10)
if (currentPage == None):
page=1
strat = (int(currentPage) - 1)*10
if paginator.num_pages > 11:
if currentPage - 5 < 1:
pageRange = range(1, 11)
elif currentPage + 5 > paginator.num_pages:
pageRange = range(paginator.num_pages - 9, paginator.num_pages + 1)
else:
pageRange = range(currentPage - 5, currentPage + 5)
else:
pageRange = paginator.page_range
try:
comment_obj = paginator.page(currentPage)
except PageNotAnInteger:
comment_obj = paginator.page(1)
except EmptyPage:
comment_obj = paginator.page(1)
return render(request,'index.html,local())
我这里前后端没有分离,因为公司的前端是个菜逼。
前端模板:
<div>
{{ for i in comment_obj }}
楼层:{{ forloop.counter|add:strat }}
{{ endfor }}
</div>