必知的技术知识:django自定义分页器

简介: 必知的技术知识:django自定义分页器

app01 --> page


#自定义分页


#官方推荐,页码数为奇数


class PageNation:


def init(self,base_url,current_page_num,total_counts,request,per_page_counts=10,page_number=5,):


'''


:param base_url: 分页展示信息的基础路径


:param current_page_num: 当前页页码


//代码效果参考:http://www.jhylw.com.cn/011324707.html

:param total_counts: 总的数据量

:param per_page_counts: 每页展示的数据量


:param page_number: 显示页码数


'''


self.base_url = base_url


self.current_page_num = current_page_num


self.total_counts = total_counts


self.per_page_counts = per_page_counts


self.page_number = page_number


self.request = request


try:


self.current_page_num = int(self.current_page_num)


except Exception:


self.current_page_num = 1


if self.current_page_num < 1:


self.current_page_num = 1


half_page_range //代码效果参考:http://www.jhylw.com.cn/262834415.html

= self.page_number // 2

# 计算总页数


self.page_number_count, a = divmod(self.total_counts, self.per_page_counts)


if a:


self.page_number_count += 1


if self.current_page_num > self.page_number_count:


self.current_page_num = self.page_number_count


if self.page_number_count <= self.page_number:


self.page_start = 1


self.page_end = self.page_number_count


else:


if self.current_page_num <= half_page_range: #2


self.page_start = 1


self.page_end = page_number #5


elif self.current_page_num + half_page_range >= self.page_number_count:


self.page_start = self.page_number_count - self.page_number + 1


self.page_end = self.page_number_count


else:


self.page_start = self.current_page_num - half_page_range


self.page_end = self.current_page_num + half_page_range


import copy


from django.http.request import QueryDict


self.params = copy.deepcopy(request.GET)


# params【'page'】 = current_page_num


# query_str = params.urlencode()


#数据切片依据,起始位置


@property


def start_num(self):


start_num = (self.current_page_num - 1) self.per_page_counts


return start_num


#数据切片依据,终止位置


@property


def end_num(self):


end_num = self.current_page_num self.per_page_counts


return end_num


# 拼接HTMl标签


def page_html(self):


tab_html = ''


tab_html += ''


#首页


self.params【'page'】 = 1


showye = '首页'.format(self.base_url,self.params.urlencode())


tab_html += showye


# 上一页


if self.current_page_num == 1:


previous_page = '«'


else:


self.params【'page'】 = self.current_page_num - 1


previous_page = '«'.format(


self.base_url,self.params.urlencode())


tab_html += previous_page


#循环生成页码标签


for i in range(self.page_start, self.page_end + 1):


if self.current_page_num == i:


else:


one_tag = '{1}'.format(self.base_url, i,self.params.urlencode())


tab_html += one_tag


# 下一页


if self.current_page_num == self.page_number_count:


next_page = '»'


else:


self.params【'page'】 = self.current_page_num + 1


next_page = '»'.format(self.base_url, self.params.urlencode())


tab_html += next_page


# 尾页


self.params【'page'】 = self.page_number_count


weiye = '尾页'.format(


self.base_url, self.params.urlencode())


tab_html += weiye


tab_html += ''


return tab_html


#函数low鸡版


def pagenation(base_url,current_page_num,total_counts,per_page_counts=10,page_number=5):


'''


total_counts数据总数


per_page_counts每页分多少条数据


page_number = 页码显示多少个


current_page_num 当前页


:return:


'''


# all_objs_list = models.Customer.objects.all()


# total_counts = all_objs_list.count()


# page_number = 5


try:


current_page_num = int(current_page_num)


except Exception:


current_page_num = 1


half_page_range = page_number//2


#计算总页数


page_number_count,a = divmod(total_counts,per_page_counts)


if current_page_num < 1:


current_page_num = 1


if a:


page_number_count += 1


if current_page_num > page_number_count:


current_page_num = page_number_count


start_num = (current_page_num - 1) 10


end_num = current_page_num 10


if page_number_count <= page_number:


page_start = 1


page_end = page_number_count


else:


if current_page_num <= half_page_range:


page_start = 1


page_end = page_number


elif current_page_num + half_page_range >= page_number_count:


page_start = page_number_count - page_number + 1


page_end = page_number_count


else:


page_start = current_page_num - half_page_range


page_end = current_page_num + half_page_range


#拼接HTMl标签


tab_html = ''


tab_html += ''


#上一页


if current_page_num == 1:


previous_page = '«'


else:


previous_page = '«'.format(base_url,current_page_num-1)


tab_html += previous_page


for i in range(page_start,page_end+1):


if current_page_num == i:


one_tag = '{1}'.format(base_url,i)


else:


one_tag = '{1}'.format(base_url, i)


tab_html += one_tag


#下一页


if current_page_num == page_number_count:


next_page = '»'


else:


next_page = '»'.format(base_url,current_page_num+1)


tab_html+=next_page


tab_html += ''


return tab_html,start_num,end_num


app01--> views


from django.shortcuts import render,HttpResponse,redirect


import os


from django.contrib import auth


from app01.models import UserInfo


from crm import settings


from django.views import View


from django import forms


from app01 import models


from django.utils.decorators import method_decorator


from django.contrib.auth.decorators import login_required


# Create your views here.


class RegForm(forms.Form):


username = forms.CharField(


label='用户名',


max_length=12,


min_length=6,


error_messages={


'max_length':'用户名不能超过12位',


'min_length':'用户名不能低于6位',


'required':'用户名不能为空',


}


)


password = forms.CharField(


label='密码',


max_length=16,


min_length=6,


error_messages={


'max_length': '密码不能超过12位',


'min_length': '密码不能低于6位',


'required': '密码不能为空',


},


widget=forms.widgets.PasswordInput(render_value=True),


)


r_password = forms.CharField(


label='密码',


max_length=16,


min_length=6,


error_messages={


'max_length': '密码不能超过12位',


'min_length': '密码不能低于6位',


'required': '密码不能为空',


},


widget=forms.widgets.PasswordInput,


)


def init(self,args,**kwargs):


super().init(args,**kwargs)


for field in self.fields:


self.fields【field】.widget.attrs.update({'class':'form-control'})


def get_valid_img(request):


from PIL import Image


#终极版,方式5


import random


def get_random_color():


return (random.randint(0,255),random.randint(0,255),random.randint(0,255))


from PIL import Image,ImageDraw,ImageFont


img_obj = Image.new('RGB', (236, 34), get_random_color()) #图片对象


draw_obj = ImageDraw.Draw(img_obj) #通过图片对象生成一个画笔对象


font_path = os.path.join(settings.BASE_DIR,'statics/font/ziti.TTF') #获取字体,注意有些字体文件不能识别数字,所以如果数字显示不出来,就换个字体


font_obj = ImageFont.truetype(font_path,16) #创建字体对象


sum_str = '' #这个数据就是用户需要输入的验证码的内容


for i in range(6):


a = random.choice(【str(random.randint(0,9)), chr(random.randint(97,122)), chr(random.randint(65,90))】) #4 a 5 D 6 S


sum_str += a


# print(sum_str)


draw_obj.text((84,10),sum_str,fill=get_random_color(),font=font_obj) #通过画笔对象,添加文字


Width</span>=236


Height</span>=34


# 添加噪线

相关文章
|
6月前
|
JavaScript 数据库 Python
django实现增删改查分页接口
django实现增删改查分页接口
|
5月前
|
编解码 前端开发 JavaScript
技术经验分享:Django学习日记
技术经验分享:Django学习日记
|
5月前
|
NoSQL Redis Python
技术经验分享:Django实现websocket
技术经验分享:Django实现websocket
33 0
|
2月前
|
数据库 Python
django中数据库外键可以自定义名称吗
django中数据库外键可以自定义名称吗
|
3月前
|
前端开发 关系型数据库 Python
Django入门到放弃之分页器
Django入门到放弃之分页器
|
3月前
|
安全 数据库 数据安全/隐私保护
|
3月前
|
安全 中间件 项目管理
Django 后端架构开发:分页器到中间件开发
Django 后端架构开发:分页器到中间件开发
41 1
|
2月前
|
数据处理 Python
Django视图:构建动态Web页面的核心技术
Django视图:构建动态Web页面的核心技术
|
3月前
|
SQL Shell API
python Django教程 之 模型(数据库)、自定义Field、数据表更改、QuerySet API
python Django教程 之 模型(数据库)、自定义Field、数据表更改、QuerySet API
|
3月前
|
中间件 API 网络架构
Django后端架构开发:从匿名用户API节流到REST自定义认证
Django后端架构开发:从匿名用户API节流到REST自定义认证
37 0