如何实现在Django年末减少分页(How to implement end less pagina

2019-10-29 05:07发布

我想实现在Django应用程序年末减少分页,但停留在如何实现叽叽喳喳像年末减少滚动:

我的models.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:

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”模板:

<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:

<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 }}在给定的教程 .Django给出了这样的错误'BlockNode' object has no attribute 'context' ,如果我删除{{ block.super }} 我得到一个和前一个功能简单的分页。

有人能帮助我吗。 我想在滚动负载分页实现...

Answer 1:

请尝试一下我的代码:

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:

<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:

{% 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项到您的DB正确地检查出来...



Answer 2:

你必须包括page_template调用之前{{block.super}}

<h2>Entries:</h2>
{% include page_template %}

{% block js %}
    {{ block.super }}
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="{{ STATIC_URL }}endless_pagination/js/endless-pagination.js"></script>
    <script>$.endlessPaginate();</script>
{% endblock %}


文章来源: How to implement end less pagination in Django