简体   繁体   English

Django模板中的动态分页

[英]Dynamic Pagination in Django Templates

I am trying to achieve a style in css3 with dynamic pagination suing Django as backend. 我正在尝试使用Django作为后端的动态分页在css3中实现一种样式。 I don't know how to make it happen as I am not able to make it work with while adding form to get user preference. 我不知道如何实现它,因为在添加表单以获取用户首选项时无法使用它。 Also having hard time to work with css3 to exactly rebuild the form removing button group option to get pagination. 也很难与css3一起使用,以准确地重建表单删除按钮组选项以获得分页。

Following is what I need to achieve: 以下是我需要实现的目标: 在此处输入图片说明

Views.py: Views.py:

class ProductListView(ListView):
    model = Product
    paginate_by = 12

    def get_paginate_by(self, queryset):
        return self.request.GET.get('paginate_by', self.paginate_by)

Original Template HTML5 code: 原始模板HTML5代码:

<div class="filter-show btn-group">
    <label data-translate="collections.toolbar.show">Show</label>
    <button class="btn btn-2 dropdown-toggle" data-toggle="dropdown">
      <i class="icon-exchange"></i>

      <span>12</span>

      <i class="icon-chevron-down"></i>
    </button>

    <ul class="dropdown-menu" role="menu">


      <li  class="active" ><a href="12">12</a></li>
      <li ><a href="16">16</a></li>
      <li ><a href="32">32</a></li>
      <li ><a href="all" data-translate="collections.toolbar.all">All</a></li>         
    </ul>
  </div>

What I am trying to do is: 我正在尝试做的是:

<div class="filter-show btn-group">

            <label>Show</label>
            <button class="btn btn-2 dropdown-toggle" >

              <i class="icon-exchange"></i>

              <span>12</span>

              <i class="icon-chevron-down"></i>
            </button>
            <form action="" method="get">
            <select class="dropdown-menu" role="menu" name="paginate_by">
              <option class="active" ><a href="12">12</a></option>
              <option><a href="16">16</a></option>
              <option><a href="32">32</a></option>
              <option><a href="all" data-translate="collections.toolbar.all">All</a></option>

            </select>
                </form>
          </div>

Related css code is following: 相关的css代码如下:

<style>
                .toolbar .btn-group.filter-show { margin-left: 10px; }

                .toolbar button.dropdown-toggle {
                float: none;
                border: 1px solid #cbcbcb;
                color: #505050;
                background: #fff;
                line-height: 34px;
                padding: 0 50px 0 10px;
                position: relative;
                text-transform: capitalize;
                width: 170px;
                text-overflow: ellipsis;
                white-space: nowrap;
                overflow: hidden;
                }
                .toolbar .filter-show button.dropdown-toggle {
                width: 120px;
                }
                .btn-group>.btn:last-child:not(:first-child), .btn-group>.dropdown-toggle:not(:first-child) {
                border-top-left-radius: 0;
                border-bottom-left-radius: 0;
                }
                </style>

There are two type of views on HTML5 side. HTML5方面有两种视图。 First button is Grid and Second button is List from left. 第一个按钮是网格,第二个按钮是从左开始的列表。 So if user selects Grid then all the columns in "show" area will be changed to grid of 12, 16, 32 and all. 因此,如果用户选择“网格”,则“显示”区域中的所有列都将更改为12、16、32等所有网格。 If selected List then accordingly list of 12, 16, 32 and all. 如果选择了列表,则将列出12、16、32和所有列表。 Let me know about solution. 让我知道解决方案。

Its all event based, lets say you a default view as grid view and number of items to be displayed is 12, so when you change either of the two parameter shoot out a ajax request and get template rendered as per the new parameters and replace the existing part of page where you are showing items with new template. 它基于所有事件,可以说您是默认视图,即网格视图,要显示的项目数为12,因此,当您更改两个参数中的任何一个时,都会发出ajax请求并按照新参数呈现模板并替换页面的现有部分,您将在其中显示带有新模板的项目。

For eg. 例如。

<div class=items"></div>

is where all the items are listed, so on change of every parameter you can just replace contents of items div with the a template rendered as per new parameters for pagination. 是列出所有项目的位置,因此在更改每个参数时,您只需将div的内容替换为根据新参数进行分页呈现的模板即可。

Feel free to ask questions, in case I might have misunderstood your problem or I am being unclear. 如果我可能误解了您的问题或不清楚,请随时提出问题。

Edit: 编辑:

In your Django views, I assume you know how to pagination module, there you can mention the number of products you want to see in a single page. 在您的Django视图中,我假设您知道如何进行分页模块,您可以在其中提及要在单个页面中看到的产品数量。 So, for example lets say you need 16 products per page, so you use paginator accordingly. 因此,例如,假设您每页需要16种产品,因此您可以相应地使用分页器。 Following this you can simple use template based on user's selection and pass the list of products from you view to that template. 接下来,您可以根据用户的选择简单使用模板,并将产品列表从视图传递到该模板。

For reference, Django pagination 供参考, Django分页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM