即时更改paginate_by的值 [英] Change value for paginate_by on the fly

查看:71
本文介绍了即时更改paginate_by的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够允许用户更改默认页面大小(paginate_by).我当前的页面大小设置为10;我希望有25、50等所有按钮.

I would like to be able to allow users change the default page size (paginate_by). My current page size is set to 10; I'd like to have buttons for say 25, 50, and all.

我正在使用PostgreSQL 11.4运行Django 2.2和Python 3.73.我的views.py创建一个查询集并设置分页.我的prod_list.html模板正确地在页面中显示数据,每页十行.

I am running Django 2.2 and Python 3.73 with postgresql 11.4. My views.py creates a queryset and sets up pagination. My prod_list.html template properly displays the data in pages with ten rows per page.

这是我目前在views.py中拥有的内容:

Here is what I currently have in my views.py:

class ProdViewSet(viewsets.ModelViewSet):

    queryset = Proddetails.objects.all()
    serializer_class = ProdSerializer


class ProdListView(ListView):
    model = Proddetails
    template_name = 'prod_list.html'
    # added for pagination
    context_object_name =  'prods' #Default: object_list
    paginate_by = 10

我希望能够让我的用户更改每页的行数.我在想我需要根据用户单击哪个按钮来重置 paginate_by ,但是我该怎么做?

I want to be able to let my users change the number of rows per page. I'm thinking I need to reset paginate_by based on which button the user clicks, but how can I do that?

谢谢-

Al

好吧,我试图发表评论,但这没有给我足够的空间.我希望这是正确的方法:

Well, I tried to comment, but that didn't give me enough room. I hope this is the correct way to do this:

非常感谢您;做到了.但是,我对其进行了修改,以便为我提供带有页码的按钮,并且出现了奇怪的现象.我有一个for循环,可用来将页码作为按钮:

Thank you very much; that did the trick. I modified it to give me buttons with the page numbers, though, and I'm getting an odd behavior. I have a for loop that I use to put out the page numbers as buttons:

{% for i in DataPaginated.paginator.page_range %}
  {% if DataPaginated.number == i %}
    <button class="w3-button w3-amber">{{ i }} </button>
  {% else %}
    <a href="?page={{ i }}" class="w3-button">{{ i }}</a>
  {% endif %}
{% endfor %}

如果将分页设置为默认值十,则此方法将正常工作并显示三页(共有24个项目).如果我将分页更改为20,则它看起来一开始就正确,并显示了两页.但是,如果我单击第二页,它会将我的分页更改回10,再次显示三页,然后将我放在三页中的第二页上.我可以做些什么来使分页锁定在表单选择的设置上吗?

If the pagination is set to the default of ten this works properly and shows three pages (there are 24 items). If I change the pagination to 20, it looks right at first and shows two pages. However, if I click on the second page, it changes my pagination back to 10, shows three pages again, and places me on the 2nd of the three pages. Is there something I can do to keep my pagination locked at the setting chosen with the form?

推荐答案

class ProdListView(ListView):
    model = Proddetails
    template_name = 'prod_list.html'
    # added for pagination
    context_object_name =  'prods' #Default: object_list
    paginate_by = 10

    def get(self, request):

        paginate_by = request.GET.get('paginate_by', 10) or 10
        data = self.model.objects.all()

        paginator = Paginator(data, paginate_by)
        page = request.GET.get('page')

        try:
            paginated = paginator.get_page(page)
        except PageNotAnInteger:
            paginated = paginator.get_page(1)
        except EmptyPage:
            paginated = paginator.page(paginator.num_pages)

        return render(request, self.template_name, {'DataPaginated':paginated, 'paginate_by':paginate_by})

您应该将其放在模板中,以允许用户选择分页.您可以使用GET方法通过表格发送分页号码.视图可能会得到它.

you should put this in your template to allow to the user to select the pagination. you may send the number of paginate by the form using the method GET. the view may get it.

视图一旦获得通过方法GET传递的数据(分页号),它将处理请求,然后给您一个响应,其中包括您应在模板中呈现的分页数据.

once the view get the data (paginate number) passed by method GET it process the request then it give you a response that include the paginate data you should render in the template.

<form method="GET">
    <select name="paginate_by" id="">
        <option value="10">10</option>
        <option value="20">10</option>
        <option value="30">30</option>
        <option value="50">50</option>
    </select>
    <input type="submit" value="Paginate">
</form>

<!-- RENDER YOU LIST HERE. Now context_object_name must be DataPaginated or whatever name you call it-->

<div class="pagination">
    <span class="step-links">
        {% if DataPaginated.has_previous %}
            <a href="?page=1&param={{param}}">&laquo; first</a>
            <a href="?page={{ DataPaginated.previous_page_number }}&param={{param}}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ DataPaginated.number }} of {{ DataPaginated.paginator.num_pages }}.
        </span>

        {% if DataPaginated.has_next %}
            <a href="?page={{ DataPaginated.next_page_number }}&param={{param}}">next</a>
            <a href="?page={{ DataPaginated.paginator.num_pages }}&param={{param}}">last &raquo;</a>
        {% endif %}
    </span>
</div>

Django文档分页

这篇关于即时更改paginate_by的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆