SoFunction
Updated on 2024-10-26

Django realize the simple paging function of the method details

In this article, an example of Django to achieve a simple paging function. Shared for your reference, as follows:

Using django's third-party modulesdjango-pure-pagination

Install the module:

pip install django-pure-pagination

Add 'pure_pagination' to file

INSTALLED_APPS = (
  ...
  'pure_pagination',
)

in the file

from  import render
rom .models import mymodel
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
def NewsList(request):
  all_news = ().order_by('-add_time')
  # Paging function
  try:
    page = ('page', 1)
  except PageNotAnInteger:
    page = 1
  p = Paginator(all_news, 3, request=request)
  news = (page)
  return render(request, '', {'all_news': news})

You need to add '.object_list' to the parameter 'all_news' passed in the file call to view.

{% extends '' %}
{% block content %}
<ul>
{% for new in all_news.object_list %}
  <li>{{}}</li>
{% endblock %}
</ul>

The part that implements the page turn:

<div class="pageturn">
  <ul class="pagelist">
    {% if all_news.has_previous %}
      <li class="long"><a href="?{{ all_news.previous_page_number.querystring }}" rel="external nofollow" >preceding page</a></li>
    {% endif %}
    {% for page in all_news.pages %}
      {% if page %}
        {% ifequal page all_news.number %}
          <li class="active"><a href="?{{  }}" rel="external nofollow" rel="external nofollow" >{{ page }}</a></li>
        {% else %}
          <li><a href="?{{  }}" rel="external nofollow" rel="external nofollow" class="page">{{ page }}</a></li>
        {% endifequal %}
      {% else %}
        <li class="none"><a href="">...</a></li>
      {% endif %}
    {% endfor %}
    {% if all_news.has_next %}
      <li class="long"><a href="?{{ all_news.next_page_number.querystring }}" rel="external nofollow" >next page</a></li>
    {% endif %}
  </ul>
</div>

The styles are much simplified from those provided in the documentation for ease of use.

.pageturn .pagelist {
  display: table-cell;
  vertical-align: middle;
  overflow: hidden;
}
.pageturn li {
  width: 30px;
  height: 30px;
  line-height: 30px;
  margin-left: 10px;
  float: left;
  text-align: center;
}
.pageturn li:first-child {
  margin-left: 0;
}
.pageturn li:hover a, .pageturn .active a {
  background: #717171;
  color: #fff;
  border-color: #eaeaea;
}
.pageturn a {
  border: 1px solid #eaeaea;
  display: block;
  height: 28px;
  color: #6c6c6c;
}
.pageturn .long {
  width: 100px;
}
.pageturn .none a {
  border: 0;
}
.pageright {
  float: right;
  width: auto;
  display: inline;
  clear: none;
  margin-top: 10px;
}

Readers interested in more Python related content can check out this site's topic: thePython Data Structures and Algorithms Tutorial》、《Summary of Python encryption and decryption algorithms and techniques》、《Summary of Python coding manipulation techniques》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.