如何在使用django-pagination时显示正确的对象编号

p1tboqfb  于 2023-01-06  发布在  Go
关注(0)|答案(4)|浏览(140)

我正在使用django-pagination对我的对象列表进行分页。它工作得非常完美。我想给予页面上的每个对象一个数字,我使用{{forloop.counter}}来实现这一点,但问题是它在每页上从1开始计数对象。我想显示实际的对象计数。
假设我每页分页10个对象,那么我希望第2页的第一个对象编号为11。我尝试为此编写一个模板过滤器,但不知何故,我无法将request.get.page{{forloop.counter}}发送到我的过滤器函数。我无法这样做。
任何方向的帮助将不胜感激。

uidvcgyl

uidvcgyl1#

我已经在我的模板中使用了这个,它工作正常

{{ page_obj.start_index|add:forloop.counter0 }}

或者你可以用

{% for obj in obj_list %}
     {{ forlopp.counter0|add:obj_list.start_index }}
 {% endfor %}
eulz3vhy

eulz3vhy2#

可以使用add模板标记将当前计数从分页器添加到forloop

{{ forloop.counter|add:paginator.page.start_index }}
kkih6yb8

kkih6yb83#

用从零开始的for循环计数器添加分页器开始索引
在模板中

{% for object in page_objects %}
    ...
    {{ forloop.counter0|add:page_objects.start_index }}
    ...
    ...
{% endfor %}

看得见

objects = Abcdef.objects.all() # Abcdef is the modal
paginator = Paginator(objects, 10)
page_number = request.GET.get('page')
try:
    page_objects = paginator.page(page_number)
except PageNotAnInteger:
    page_objects = paginator.page(1)
except EmptyPage:
    page_objects = paginator.page(paginator.num_pages)    
data = {
    "page_objects" : page_objects,
}
return render(request, "template/template.html", data)
ljsrvy3e

ljsrvy3e4#

按照以下步骤渲染对象序列号
在这里我使用ListView来呈现对象,这是非常容易使用的。(我的模型是雇员模型)
Step1: Extend Listview in views.py and replace the model on which you want to paginate.

    • 查看次数. py**
from django.views.generic.list import ListView
from app.models import Employee

class EmployeeDetailsView(ListView):
    paginate_by = 10
    model = Employee  # model Employee with 4 fields(name, email, sex, phone)
    template_name = 'employees.html'

Step 2: Configure route in urls.pyurls.py

from .views import *
urlpatterns = [
    ...,
    path('employees/', EmployeeDetailsView.as_view(), name='get-employees'),
]

步骤3:在模板中使用变量page_objbecause渲染对象,将分页器page_obj添加到上下文中。

    • 雇员. html**
<!doctype html>
  <html>
    <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.rtl.min.css" integrity="sha384-WJUUqfoMmnfkBLne5uxXj+na/c7sesSJ32gI7GfCk4zO4GthUKhSEGyvQ839BC51" crossorigin="anonymous">

    <title>Employee Details</title>
   </head>
  <body>
<h1>Employee Table</h1>

<!-- Optional JavaScript; choose one of the two! -->

<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>

<div class="m-4 p-2">
<table class="table table-bordered">
    <thead>
      <tr>
        <th>Sr.No.</th>
        <th scope="col">Name</th>
        <th scope="col">Email</th>
        <th scope="col">Sex</th>
        <th scope="col">Contact</th>
      </tr>
    </thead>
    <tbody>
        {% for emp in page_obj %}
      <tr>
       <!--The Required solution is here -->
        <th scope="row">{{ forloop.counter0|add:page_obj.start_index }}</th>
        <td>{{emp.name}}</td>
        <td>{{emp.email}}</td>
        <td>{{emp.sex}}</td>
        <td>{{emp.phone}}</td>
        <td>{{page_obj.sta}}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>

  <nav aria-label="...">
    <ul class="pagination justify-content-end">
       {% if page_obj.has_previous %}
         <li class="page-item">
            <a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
          </li>
          <li class="page-item">
             <a class="page-link" href="?page=1" aria-label="First">
                <span aria-hidden="true">&laquo;</span>
             </a>
           </li>
            {% else %}
           <li class="page-item disabled">
              <a class="page-link">Previous</a>
           </li>
           <li class="page-item">
             <a class="page-link" href="?page=1" aria-label="First">
                <span aria-hidden="true">&laquo;</span>
              </a>
                </li>
            {% endif %}
            
            <li class="page-item">
            <a class="page-link active">
                <span class="current">
                    Page <b>{{ page_obj.number }}</b> of {{ page_obj.paginator.num_pages }}.
                </span>
            </a>
            </li>
            {% if page_obj.has_next %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}" aria-label="Last">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.next_page_number }}">next</a>
                </li>
                
            {% else %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.num_pages }}" aria-label="Last">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <li class="page-item disabled">
                    <a class="page-link">Next</a>
                </li>
            {% endif %}
        </ul>
      </nav>
  </div>
</body>
</html>

下面的代码将解决我在模板中提供的给定问题。

<th scope="row">{{ forloop.counter0|add:page_obj.start_index }}</th>

相关问题