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 %}
<li class="long"><a href="?{{ all_orgs.previous_page_number.querystring }}">上一页</a></li>
{% endif %}
{% for page in all_orgs.pages %}
{% if page %}
{% ifequal page all_orgs.number %}
<li class="active"><a href="?{{ page.querystring }}">{{ page }}</a></li>
{% else %}
<li class=""><a href="?{{ page.querystring }}">{{ page }}</a></li>
{% endifequal %}
{% else %}
<li class="none"><a href="">...</a></li>
{% endif %}
{% endfor %}
{% if all_orgs.has_next %}
<li class="long"><a href="?{{ all_orgs.next_page_number.querystring }}">下一页</a></li>
{% endif %}
7.筛选与排序功能d的html文件填写:
<div class="listoptions">
<ul>
<li>
<h2>机构类别</h2>
<div class="cont">
<a href="?city={{ city_id }}"><span
class="{% ifequal category '' %}active2{% endifequal %}">全部</span></a>
<a href="?ct=pxjg&city={{ city_id }}"><span
class="{% ifequal category 'pxjg' %}active2{% endifequal %}">培训机构</span></a>
<a href="?ct=gx&city={{ city_id }}"><span
class="{% ifequal category 'gx' %}active2{% endifequal %}">高校</span></a>
<a href="?ct=gr&city={{ city_id }}"><span
class="{% ifequal category 'gr' %}active2{% endifequal %}">个人</span></a>
</div>
</li>
<li>
<h2>所在地区</h2>
<div class="more">更多</div>
<div class="cont">
<a href="?ct={{ category }}"><span
class="{% ifequal city_id '' %}active2{% endifequal %}">全部</span></a>
{% for city in all_citys %}
<a href="?city={{ city.id }}&ct={{ category }}"><span
class="{% ifequal city_id city.id|stringformat:'i' %}active2{% endifequal %}">{{ city.name }}</span></a>
{% endfor %}
</div>
</li>
</ul>
</div>