如何在Django中实现无末尾分页 [英] How to implement end less pagination in Django
问题描述
我正在尝试在Django App中实现无末尾分页,但停留在如何实现无末尾滚动这样的twitter上:
I am trying to implement end less pagination in Django App but stuck at how to implement twitter like end less scrolling :
我的模型.py
from django.db import models
from django.contrib import admin
#------------------------------------------------------------------------------
class Book(models.Model):
name = models.CharField(max_length=50)
pub_date = models.DateField(auto_now_add=True)
class bookAdmin(admin.ModelAdmin):
"""Book admin class"""
list_display = ('name','pub_date')
ordering = ('name',)
admin.site.register(Book,bookAdmin)
我的views.py:
My views.py :
from models import Book
from django.template import RequestContext
from django.shortcuts import render_to_response
#------------------------------------------------------------------------------
def latest_books(request,template = 'latest_books.html',
page_template = 'latest_books_page.html' ):
context = {}
book_list = Book.objects.order_by('-pub_date')
context.update( {'book_list': book_list, 'page_template': page_template,} )
# override the template and use the 'page' style instead.
if request.is_ajax():
template = page_template
return render_to_response(
template, context, context_instance=RequestContext(request) )
我的"latest_books.html"模板:
My 'latest_books.html' template :
<html><head><title>Books</title></head>
<body>
<h1>Books</h1>
{% block js %}
{{ block.super }}
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.2.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="http://yourjavascript.com/337923491/endless.js" charset="utf-8"></script>
<script type="text/javascript" src="http://yourjavascript.com/151379951/endless-pagination.js"></script>
<script>$.endlessPaginate();</script>
{% endblock %}
{% block content %}
<div class="endless_page_template">
{% include page_template %}
</div>
{% endblock %}
</body></html>
我的Latest_books_page.html:
My latest_books_page.html :
<h2>Viewing All Entries</h2>
{% load endless %}
<div>
<ul>
{% paginate book_list %}
{% for book in book_list %}
<li>{{ book.name }}</li> {{ book.pub_date }}
{% endfor %}
{% show_pages %}
</ul>
</div>
如果我使用{{ block.super }},我首先面临两个问题. > tutorial .Django给出了错误'BlockNode' object has no attribute 'context'
,如果我删除了{{ block.super }}
.我可以通过下一个和上一个功能进行简单的分页.
I am facing two issues first if i use {{ block.super }}
as given in tutorial .Django gives this error 'BlockNode' object has no attribute 'context'
and if i remove {{ block.super }}
. I get simple pagination with next and previous functionality .
有人可以帮我吗.我想实现滚动加载分页...
Can someone help me please. I want to implement on scroll load pagination...
推荐答案
请尝试我的代码:
views.py:
from models import Book
from django.template import RequestContext
from django.shortcuts import render_to_response
#------------------------------------------------------------------------------
def latest_books(request,template = 'latest_books.html',
page_template = 'latest_books_page.html' ):
context = {}
book_list = Book.objects.order_by('-pub_date')
context.update( {'book_list': book_list, 'page_template': page_template,} )
# override the template and use the 'page' style instead.
if request.is_ajax():
template = page_template
return render_to_response(
template, context, context_instance=RequestContext(request) )
latest_books.html:
latest_books.html :
<html><head><title>Books</title></head>
<body>
<h1>Books</h1>
<h2>Viewing All Entries</h2>
<div class="endless_page_template">
{% include page_template %}
</div>
{% block js %}
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="{{ STATIC_URL }}js/endless_on_scroll.js"></script>
<script src="{{ STATIC_URL }}js/endless-pagination.js"></script>
<script>
$.endlessPaginate({paginateOnScroll: true,
endless_on_scroll_margin : 10,
paginateOnScrollChunkSize: 5
});</script>
{% endblock %}
</body></html>
latest_books_page.html:
latest_books_page.html :
{% load endless %}
{% paginate 10 book_list %}
{% for book in book_list %}
{{ book.name }}<br> {{ book.pub_date }}<br><br><br>
{% endfor %}
{% show_more "even more" "working" %}
尝试并让我知道...,然后在您的数据库中输入20-30个条目以正确地进行检查...
Try out and let me know ... and enter 20-30 entries into your DB to check it out properly ...
这篇关于如何在Django中实现无末尾分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!