Ahm*_*DAL 8 python django mongodb django-pagination
我需要进行真正的分页,而不是对所有已检索的数据进行分页.Django文档站点中的示例,如;
def listing(request):
contact_list = Contacts.objects.all()
paginator = Paginator(contact_list, 25) # Show 25 contacts per page
page = request.GET.get('page')
try:
contacts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
contacts = paginator.page(paginator.num_pages)
return render_to_response('list.html', {"contacts": contacts})
Run Code Online (Sandbox Code Playgroud)
此代码在所有已检索记录上分页记录.但是有一个麻烦.如果记录太多,试图检索所有记录需要花费很多时间.我需要一个解决方案来从数据库中逐页检索记录.
在Django中有另一种解决方案吗?
Dan*_*man 20
你做了一个错误的假设.Django在分页时不检索所有对象:它适当地切割查询集,在SQL上使用LIMIT和COUNT.
归档时间: |
|
查看次数: |
3525 次 |
最近记录: |