0

我使用 Django Endless Paginator 插件。我尝试从模板端更改 ENDLESS_PAGINATION_PER_PAGE 的值。默认为 10。我想实现一些用户可以在其中更改的 Dropbox(例如:10,20,50,100 个对象)。

感谢您的回答,但有些东西不起作用。

1.我有view.py

def adv_search(request):
objects = None
if request.GET.get('key'):
    form = AdvancedSearchForm(request.GET)
    if form.is_valid():
        repo = Repository()
        objects = list(repo.find_objects('*'+form.cleaned_data['key'] +'*', type=FileObject, chunksize=20))
    return render(request, 'templates/adv_search.html', {'form': form, 'objects': objects })
return render(request, 'templates/adv_search.html', {'form': AdvancedSearchForm(), 'objects': objects})

2.然后forms.py

class AdvancedSearchForm(forms.Form):
key = forms.CharField(max_length=500, label="", widget= forms.TextInput(attrs={'class': 'label'}))
show_props = forms.MultipleChoiceField(choices = (("pid", "pid"),("title", "title"),("type","type"),("source","source"),("date","date"),("publisher","publisher"),("subject","subject"),("label","label"),("cDate","cDate"),("mDate","mDate")),required=False, widget=forms.CheckboxSelectMultiple(attrs={'class': 'my-class'}))
paginator = forms.ChoiceField(choices =(('10', '10'),('20','20'), ('50','50'), ('100','100')) ,required=False, )

3. adv_search.html

  {% paginate paginator objects %}
{% for obj in objects %}    

...

<div class="paginator">
{% get_pages %}
    {{ pages.first_as_arrow }}
{% show_pages %}
    {{ pages.last_as_arrow }}
    <form method="get">
    {{ form.paginator }}
    <input type="submit" Value="Go">
     </form>
    {{ pages.total_count }} total objects
</div>

一切正常,除了选择

谢谢安德烈

4

1 回答 1

0

您可以有一个表单来设置每页的分页,然后在模板中使用该提交的值。这是一个例子:

看法

from django.shortcuts import render

from .models import Entry

def endless_view(request):
    pagination_per_page = request.GET.get('per_page', 10)
    entries = Entry.objects.all()
    return render(request, 'some_template.html', {'pagination_per_page':
                  pagination_per_page, 'entries': entries})

模板

{% extends 'base.html' %}
{% load endless %}
{% block content %}
    {#  #}
    <form action="" method="get">
        <select name="per_page">
            <option value="10">10 per page</option>
            <option value="20">20 per page</option>
            <option value="50">50 per page</option>
            <option value="100">100 per page</option>
        </select>
        <input type="submit" Value="Go">
    </form>
    {% paginate pagination_per_page entries %}
    {% for entry in entries %}
        {# your code to show the entry #}
    {% endfor %}
    {% show_pages %}
{% endblock content %}

参考

  1. http://django-endless-pagination.readthedocs.org/en/latest/templatetags_reference.html
于 2013-08-01T12:52:43.197 回答