标签:插入 lan val lis request 模型 data col current
# 用一个实例来引出分页功能 1.创建模型类 class AreaInfo(models.Model): area_name = models.CharField(max_length=50) area_code = models.CharField(max_length=50) 2.测试数据 ================================================================================ INSERT INTO app_apge_areainfo (‘area_code‘, ‘area_name‘) VALUES (110101, ‘东城区‘), (110102, ‘西城区‘), (110103, ‘崇文区‘), (110104, ‘宣武区‘), (110105, ‘朝阳区‘), (110106, ‘丰台区‘), (110107, ‘石景山区‘), (110108, ‘海淀区‘), (110109, ‘门头沟区‘), (110111, ‘房山区‘), (110112, ‘通州区‘), (110113, ‘顺义区‘), (110114, ‘昌平区‘), (110115, ‘大兴区‘), (110116, ‘怀柔区‘), (110117, ‘平谷区‘), (110228, ‘密云县‘), (110229, ‘延庆县‘), (120101, ‘和平区‘), (120102, ‘河东区‘), (120103, ‘河西区‘), (120104, ‘南开区‘), (120105, ‘河北区‘), (120106, ‘红桥区‘), (120107, ‘塘沽区‘), (120108, ‘汉沽区‘), (120109, ‘大港区‘), (120110, ‘东丽区‘), (120111, ‘西青区‘), (120112, ‘津南区‘), (120113, ‘北辰区‘), (120114, ‘武清区‘), (120115, ‘宝坻区‘), (120221, ‘宁河县‘), (120223, ‘静海县‘), (120225, ‘蓟 县‘), (130101, ‘市辖区‘), (130102, ‘长安区‘), (130103, ‘桥东区‘), (130104, ‘桥西区‘), (130105, ‘新华区‘), (130107, ‘井陉矿区‘) ================================================================================ 3.在tests.py中插入数据 from .models import * area_data = [ ... ] # 插入测试数据 for area in area_data: # 创建模型对象 a = AreaInfo() a.area_name = area[1] a.area_code = area[0] # 保存数据到数据库中】 a.save() 4.视图中 ================================================================================ def index(request): # 每页显示数据数目 show_per_page = 5 # 查询所有数据 area_list = AreaInfo.objects.all() # 创建分页器对象 paginator = Paginator(area_list, show_per_page) # 通过GET方式获得当前页码 current_page = request.GET[‘page‘] # 获得当前页的数据 current_page_data = paginator.page(current_page) # 页码区间 # paginator.page_range return render(request, ‘other/index.html‘, locals()) ================================================================================ 5.html页面中 ================================================================================ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table> {% for page_data in current_page_data %} <tr><td>{{ page_data.area_code }}</td><td>{{ page_data.area_name }}</td></tr> {% endfor %} </table> <hr> 当前是:{{ current_page }}页.<br> {# 数据的合法页码 列表类型 #} {% if current_page_data.has_previous %} <a href="?page={{ current_page_data.previous_page_number }}"><span style="font-size: 20px;">上一页</span></a> {% endif %} {% for index in paginator.page_range %} <a href="?page={{ index }}"><span style="font-size: 20px;">{{ index }}</span></a> {% endfor %} {# 判断是否有下一页 #} {% if current_page_data.has_next %} <a href="?page={{ current_page_data.next_page_number }}"><span style="font-size: 20px;">下一页</span></a> {% endif %} </body> </html> ================================================================================
标签:插入 lan val lis request 模型 data col current
原文地址:http://www.cnblogs.com/pymkl/p/7735737.html