Django分页不适用于非常大的数据集

时间:2019-03-14 11:14:58

标签: python django django-models django-pagination

当我对非常大的数据集使用分页时,它的工作速度太慢。也许我没有正确执行它。请帮忙。如何使其快速?有什么我可以更改或实施的以便它能正常工作吗?

  

view.py

def display(request):
    user_list = Final.objects.all()
    searchlen= user_list.count()
    paginator = Paginator(user_list, 100)
    page = request.GET.get('page')
    users = paginator.get_page(page)
    return render(request,'displayLogs.html',{'users': users,'searchlen':searchlen})
  

model.py

class Final(models.Model):
    name = models.TextField(db_column='Date',primary_key=True, blank=True, null=False)  # Field name made lowercase.
    corr = models.TextField(db_column='CorrelationId', blank=True, null=True)  # Field name made lowercase.

      <tbody>
	<tr class="gradeX">
	{% for item in users %}
	<td>{{item.dat}}</td>
	<td>{{item.act}}</td>
	<td>{{item.actor}}</td>
	<td class="center">{{item.tar}}</td>
	</tr>
							
	{% endfor %}
</tbody>


<div class="pagination" style="border-style:inset;border-width:5px;padding-bottom: 5px">
	{% if users.has_previous %}

	<a class="pagination-action" href="?page=1"> <i class="fas fa-angle-double-left"></i></a>

	<a class="pagination-action" href="?page={{ users.previous_page_number}}"> <i class="fas fa-angle-left"></i></a>

	{% endif %}

	{% for num in users.paginator.page_range %}
		{% if users.number == num %}

			<span class="pagination-number pagination-current"><strong>{{ num }}</strong></span>

		{% elif num > users.number|add:'-3' and num < users.number|add:'3' %}

		<a class="pagination-number" href="?page={{ num }}">{{num}}</a>

		{% endif %}

	{% endfor %}

	{% if users.has_next %}

	<a class="pagination-action" href="?page={{ users.next_page_number }}"><i class="fas fa-angle-right"></i></a>
	<a class="pagination-action" href="?page={{ users.paginator.num_pages }}"> <i class="fas fa-angle-double-right"> </i></a>

	{% endif %}
				
	</div>

1 个答案:

答案 0 :(得分:0)

我建议对大型数据集执行类似的操作。我附上我的设计如下。在这里,单击<<进入首页,>>进入最后一页,<进入上一页,>进入下一页。

<tbody>
    {% for item in users %}
      <tr class="gradeX">
        <td>{{item.dat}}</td>
        <td>{{item.act}}</td>
        <td>{{item.actor}}</td>
        <td class="center">{{item.tar}}</td>
      </tr>                 
    {% endfor %}
</tbody>
<nav aria-label="pagination" class="pagination_area">
<div class="row">
  {% if users.end_index > 0 %}
  <div class="col-sm-12 col-md-5 d-none d-md-block">
    <p>Showing {{ users.start_index }} to {{ users.end_index }} of {{ users.paginator.count}}.</p>
  </div>
  {% endif %}
  {% if users.paginator.num_pages > 1 %}
  <div class="col-sm-12 col-md-7 dataTables_pager">
    <ul class="pagination">
      {% if users.has_previous %}

        <li class="page-item">
          <a class="page-link" data-page="1" href="?page={{ users.previous_page_number }}">
            <i class="fa fa-angle-double-left"></i>
          </a>
        </li>
        {% if users.previous_page_number > 1 %}
          <li class="page-item">
            <a class="page-link " data-page="{{users.previous_page_number}}"  href="?page={{ users.previous_page_number }}">
              <i class="fa fa-angle-left"></i>
            </a>
          </li>
        {% endif %}

      {% endif %}

      {% if users.previous_page_number > 2 %}
        <li class="page-item">
          <a class="page-link " data-page="{{users.number|add:'-2'}}" href="?{{users.number|add:'-2'}}"> {{ users.number|add:"-2" }} </a>
         </li>
        <li class="page-item">
          <a class="page-link " data-page="{{users.number|add:'-1'}}" href="?page={{users.number|add:'-1'}}"> {{ users.number|add:"-1" }} </a>
        </li>
      {% endif %}

      <li class="page-item active"><span class="page-link ">{{ users.number }}</span></li>

      {% if users.paginator.num_pages > users.number|add:"2" %}
        <li class="page-item">
          <a class="page-link " data-page="{{users.number|add:'1'}}" href="?page={{users.number|add:'1'}}"> {{ users.number|add:"1" }} </a>
        </li>
        <li class="page-item">
          <a class="page-link " data-page="{{users.number|add:'2'}}" href="?page={{users.number|add:'2'}}"> {{ users.number|add:"2" }} </a>
        </li>
      {% endif %}

      {% if users.has_next %}
        <li class="page-item">
          <a class="page-link " data-page="{{users.next_page_number}}" href="?page={{ users.next_page_number }}">
            <i class="fa fa-angle-right"></i>
          </a>
        </li>

        <li class="page-item">
          <a class="page-link " data-page="{{users.paginator.num_pages}}" href="?page={{users.paginator.num_pages}}">
            <i class="fa fa-angle-double-right"></i>
          </a>
        </li>
      {% endif %}
    </ul>
  </div>
  {% endif %}
</div>
</nav>

enter image description here

这是我的设计。您可以根据需要更改设计。