1.html模版(部分)
{% block page-content %}
<table class="table">
<tr>
<th>IDth><th>QQ号th><th>姓名th><th>渠道th><th>咨询课程th>
<th>课程类型th><th>客户备注th><th>状态th><th>课程顾问th><th>日期th>
tr>
{% for item in customer_list %}
<tr>
<td>{{ item.id }}td>
<td>{{ item.qq }}td>
<td>{{ item.name }}td>
<td>{{ item.source_type}}td>
<td>{{ item.course }}td>
{# 显示字段的中文备注#}
<td>{{ item.get_course_type_display }}td>
{# 限制显示长度#}
<td>{{ item.consult_memo|truncatechars_html:80 }}td>
{# 小技巧,通过名称巧取颜色#}
<td class="{{ item.status }}">{{ item.get_status_display }}td>
<td>{{ item.consultant }}td>
<td>{{ item.date }}td>
tr>
{% endfor %}
table>
{# 采用bootstrap导航按钮#}
<nav aria-label="Page navigation">
<ul class="pagination">
{# 判断是否有上一页#}
{% if customer_list.has_previous %}
{# 有上一页则添加上一页连接#}
<li><a href="?page={{ customer_list.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">«span>
a>li>
{% endif %}
{# 依次显示每一页,customer_list.paginator.page_range为可迭代range结果#}
{% for i in customer_list.paginator.page_range %}
{% if i == customer_list.number %}
<li class="active"><a href="?page={{ i }}">{{ i }}a>li>
{% else %}
<li class=""><a href="?page={{ i }}">{{ i }}a>li>
{% endif %}
{% endfor %}
{# 同理判断是否有下一页#}
{% if customer_list.has_next %}
<li><a href="?page={{ customer_list.next_page_number }}" aria-label="Next">
<span aria-hidden="true">»span>
a>li>
{% endif %}
ul>
nav>
{% endblock %}
view.py
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def customers(request):
# 获取所有信息
customer_list = models.Customer.objects.all()
# 每页三条分页
paginator = Paginator(customer_list, 3)
page = request.GET.get('page')
try:
# 获取指定页内容
contacts = paginator.page(page)
except PageNotAnInteger:
contacts = paginator.page(1)
except EmptyPage:
contacts = paginator.page(paginator.num_pages)
return render(request, 'crm/customers.html', {'customer_list': contacts})