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 %}