1 知识储备
1.1 request.GET是什么类型?
- 视图函数
def test(request):
print(request.GET) # **<QueryDict: {'fd': ['1'], 'fc': ['2']}>**
print(type(request.GET)) # <class 'django.http.request.QueryDict'>
return HttpResponse('test!')
- QueryDict不能修改
def test(request):
print(request.GET)
print(type(request.GET))
params = request.GET
params['fd'] = 2 # AttributeError: This QueryDict instance is immutable
return HttpResponse('test!')
- QueryDict 怎么修改
设置QueryDict的_mutable 属性为True,
def test(request):
print(request.GET) #<QueryDict: {'fd': ['1'], 'fc': ['2']}>
print(type(request.GET)) #<class 'django.http.request.QueryDict'>
params = request.GET
params._mutable = True
params['fd'] = 4 # AttributeError: This QueryDict instance is immutable
print(params) #<QueryDict: {'fd': [4], 'fc': ['2']}>
return HttpResponse('test!')
- urlencode函数
def test(request):
print(request.GET) #<QueryDict: {'fd': ['1'], 'fc': ['2']}>
print(type(request.GET)) #<class 'django.http.request.QueryDict'>
params = request.GET
params._mutable = True
params['fd'] = 4 # AttributeError: This QueryDict instance is immutable
print(params) #<QueryDict: {'fd': [4], 'fc': ['2']}>
print(params.urlencode()) # fd=4&fc=2
return HttpResponse('test!')
- 一般为了不影响原参数,需要对QueryDict进行深拷贝
def test(request):
print(request.GET)
print(type(request.GET))
params = request.GET
params_copy = params.copy()
print(id(params_copy)) # 2212784354256
print(id(params)) # 2212783562784
params._mutable = True
params['fd'] = 4 # AttributeError: This QueryDict instance is immutable
print(params)
print(params.urlencode()) #
return HttpResponse('test!')
1.2 urlib库中的urlencode
from urllib.parse import urlencode
a = {'name': 'zhangsan', 'age': 12, 'gender': 'man'}
print(urlencode(a)) # name=zhangsan&age=12&gender=man
跳转页面的时候一定要保留原URL中既有的参数,不能跳转页面,导致之前的查询页面丢失。
2 代码
"""
分页组件
"""
class Pagination(object):
def __init__(self, current_page, all_count, base_url, query_params, per_page=20, pager_page_count=11):
"""
分页初始化
:param current_page: 当前页码
:param per_page: 每页显示数据条数
:param all_count: 数据库中总条数
:param base_url: 基础URL
:param query_params: QueryDict对象,内部含所有当前URL的原条件
:param pager_page_count: 页面上最多显示的页码数量
"""
self.base_url = base_url
try:
self.current_page = int(current_page)
if self.current_page <= 0:
raise Exception()
except Exception as e:
self.current_page = 1
self.query_params = query_params
self.per_page = per_page
self.all_count = all_count
self.pager_page_count = pager_page_count
pager_count, b = divmod(all_count, per_page)
if b != 0:
pager_count += 1
self.pager_count = pager_count
half_pager_page_count = int(pager_page_count / 2)
self.half_pager_page_count = half_pager_page_count
@property
def start(self):
"""
数据获取值起始索引
:return:
"""
return (self.current_page - 1) * self.per_page
@property
def end(self):
"""
数据获取值结束索引
:return:
"""
return self.current_page * self.per_page
def page_html(self):
"""
生成HTML页码
:return:
"""
# 如果数据总页码pager_count<11 pager_page_count
if self.pager_count < self.pager_page_count:
pager_start = 1
pager_end = self.pager_count
else:
# 数据页码已经超过11
# 判断: 如果当前页 <= 5 half_pager_page_count
if self.current_page <= self.half_pager_page_count:
pager_start = 1
pager_end = self.pager_page_count
else:
# 如果: 当前页+5 > 总页码
if (self.current_page + self.half_pager_page_count) > self.pager_count:
pager_end = self.pager_count
pager_start = self.pager_count - self.pager_page_count + 1
else:
pager_start = self.current_page - self.half_pager_page_count
pager_end = self.current_page + self.half_pager_page_count
page_list = []
if self.current_page <= 1:
prev = '<li><a href="#">上一页</a></li>'
else:
self.query_params['page'] = self.current_page - 1
prev = '<li><a href="%s?%s">上一页</a></li>' % (self.base_url, self.query_params.urlencode())
page_list.append(prev)
for i in range(pager_start, pager_end + 1):
self.query_params['page'] = i
if self.current_page == i:
tpl = '<li class="active"><a href="%s?%s">%s</a></li>' % (
self.base_url, self.query_params.urlencode(), i,)
else:
tpl = '<li><a href="%s?%s">%s</a></li>' % (self.base_url, self.query_params.urlencode(), i,)
page_list.append(tpl)
if self.current_page >= self.pager_count:
nex = '<li><a href="#">下一页</a></li>'
else:
self.query_params['page'] = self.current_page + 1
nex = '<li><a href="%s?%s">下一页</a></li>' % (self.base_url, self.query_params.urlencode(),)
page_list.append(nex)
page_str = "".join(page_list)
return page_str