博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分页功能的实现
阅读量:6691 次
发布时间:2019-06-25

本文共 2849 字,大约阅读时间需要 9 分钟。

1.在github上面搜索pure pagination

2.进入 django-pure-pagination

3.进入虚拟环境 安装 pip install django-pure-pagination

4.在settings里面:

INSTALLED_APPS 里面添加:

'pure_pagination', 然后在最后面添加:
'''分页''' PAGINATION_SETTINGS = {
'PAGE_RANGE_DISPLAYED': 10, 'MARGIN_PAGES_DISPLAYED': 2, 'SHOW_FIRST_PAGE_WHEN_INVALID': True, } 5.在views.py文件里面添加:
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
class OrgView(View):     def get(self, request):         #  课程机构         all_orgs = CourseOrg.objects.all()         hot_orgs = all_orgs.order_by('-click_nums')[:3]         #  城市         all_citys = CityDict.objects.all()         # 搜索功能         search_keywords = request.GET.get('keywords', '')         if search_keywords:             all_orgs = all_orgs.filter(                 Q(name__icontains=search_keywords) | Q(desc__icontains=search_keywords))         '''筛选城市'''         city_id = request.GET.get('city', '')         if city_id:             all_orgs = all_orgs.filter(city_id=int(city_id))         '''类别筛选'''         category = request.GET.get('ct', '')         if category:             all_orgs = all_orgs.filter(category=category)         sort = request.GET.get('sort', '')         if sort == 'students':             all_orgs = all_orgs.order_by('-students')         elif sort == 'courses':             all_orgs = all_orgs.order_by('-course_nums')         org_nums = all_orgs.count()         '''对内容经行分页'''         try:             page = request.GET.get('page', 1)         except PageNotAnInteger:             page = 1         p = Paginator(all_orgs, 3, request=request)         orgs = p.page(page)         content = {
'all_orgs': orgs, 'all_citys': all_citys, 'org_nums': org_nums, 'city_id': city_id, 'category': category, 'hot_orgs': hot_orgs, 'sort': sort, } return render(request, 'org-list.html', content) 6.在html文件里面填写:
{% if all_orgs.has_previous %}     
  • 上一页
  • {% endif %} {% for page in all_orgs.pages %} {% if page %} {% ifequal page all_orgs.number %}
  • {
    { page }}
  • {% else %}
  • {
    { page }}
  • {% endifequal %} {% else %}
  • ...
  • {% endif %} {% endfor %} {% if all_orgs.has_next %}
  • 下一页
  • {% endif %} 7.筛选与排序功能d的html文件填写:

    转载于:https://www.cnblogs.com/chenyang13677/p/7773293.html

    你可能感兴趣的文章