zoukankan      html  css  js  c++  java
  • day70

    d自定义分页

    保存搜索条件

    搜索用户信息

    公户和私户

    批量操作

    1 xxpy添加100个

    os  main   os.envion.setting  import django  dj.setup()		from app import models
    l1 = []
    for i in range(1,101):
    	obj = models.Customer()
    	qq = 下面的 ''.join([str(i) for i in random.choices(range(1,10),k = 11)])
    	name = 'hua'+str(i)
    	sex = random.choice(['male','fem'])
    	source = rand.ch(['qq','ref','we'])
    	course= r.c(['li','py'])
    l1.appedn(obj)
    models.custom.objects.bulk_create(*l1)
    

    random测试

    imp random random.choices(range(1,10),k= 11)[1,2.]
    k1 = [str(i) for i in l1]
    l1 = ''.join(l1)  2132434435
    
    

    views.py

    def customer():# 筛选,yixiong看不到小黑
    	customer_page_num =requ.GET.get('page',1)
    	all_customers = models.Customer.obj.filter(con=True)
        mod.Cu.ob.filter(csultant__isnull=True)
    	#判断是不是空的字符串  公户	
    	return render(req,'custom.html',lo)
    #私户
    def mycustomers(reque):
    	my_customer = m.C.o.filter(consultant=request.user)#对象
    	return render(req,'mycust.html',lo)
    
    from nbapp import page
    def test(request):
    	#太多了
    	current_page_num = request.GET.get('page',1)
    	#0 : 10
    	#10 : 20
    		num-1 *10 num*10
    	start_num = int(cunrr_page_num)-1)*10 
    	end_num = int()*10 
    	
    	page_number = 10 
    	all_data = m.c.o.all() 
    	total_count = all_data.count()
    	
    	
    	all_data = m.c.o.all()[start,end]
    	
    	ret_html ,start_num,end_num= page.pagenation(request.path,current_page_num,total_count,per_page_counts,page_number)路径不带参数	#传过路径
    				
    	return render(request,test.html,locals())
    	
    实例化能够实例化一个标签的功能
    def register(requ):
    	form_obj = UserForm()
    	if request.method == 'GET':	
    	
    	else:
    		form_obj = UserForm(req.PO)校验了{‘name’:'122',''...}
    	if form_obj.is_valid():  #验证
    		data = from_obj.clean_data 都是存正确的	所有的检测一遍才完,   
    		return redirect('login')
    	else :
        	return render(request,'re',lo)
        	重新刷新返回前端了显示错误
    前端 field.errors.0第一个错误  .2 第三个 
    

    test.html

    table	
    	th
    	tr th 
    	{% for data in all_data
    		data.name
    		
    	{ret_Html|safe}
    

    mycustomers

    {extends 'base.html %}
    
    

    base.html

    fa fa-link	我的客户
    url {'mycustomers'} 客户信息到yixiong
    

    urls.py

    #注销
    url(r'^logout/',views.logout,name='l')
    #公户
    url(r'^customers/list/',v.custom,name='cust'),	
    #私户
    url(r'^customers/list/',v.custom,name='cust'),
    

    分页 :

    page.py 测试

    from nbapp import models
    def pagenation(base_url,current_pag,total_counts,per_page = 10 ,page_num=5):
    	'''
    	total数据总数
    	per_page_counts每页分多少条
    	current_page_num当前页
    	page_num 	=页码显示多少页	
    	'''
    	#all_obj_li=mols.Cust.objs.all()		#不写死,用别的book呢? 不在这写 
    	total_coutn = all_o.count()
    	per_page_counts = 10 
    	
    	try :
    		current_page_num = int(current_page_num)
    	except Exception:
        	start_num = 1 
      	
        if current_page_num <1 :
        	current_page_num =1 
    	if 
        	
        
    	page_number = 
    	总页数
    	s,y=divmod(total_cnt,per_page) 商余   
    	half_page_range = page_numb//2 整除	 
    上一页	  
    	if a:   
    		page_num += 1       
    	#页码数显示 多少条   
    	if page_number_count <= page_numbe:   
    		pag = 1              
    		page_end = page_num   
    	else:
    		if current_page < half_page:
        	page_start = 1
        	page_end = page_number
    	    elif cu+half > page_number:
    	      page_start= page_nu -page_num 
    	    else:
            	pae_st = cu - half
            	pae_end =cu + half
            	
    	tab_html = ''
    	tab_html+= '<>'	当前页
    	if current_page_num ==1 :
    		previous_page = 'li<a disable href ='#' al
    	else:		
    		previous_page = 'li<a href ='href = '{0}' page={1} -1 al.format(base_url,i)
    
    	for i in range(1,page_num+1):取不到
    		one_page = 'li a href = {0} class='active' page={1}> {1}夹的页数'.format(base_url,i)
    		+%i
            table_html+= ''
            
    下一页      
    	if current_page_num == page_number_count:
    		nuex_page = 'li disable'
    	else:
    		next_page = .format(base_url,current_page_num+1)
    		
    	return table_html  ,start_num,end_num 
        
        
        
    封装成一个类
    class PageNation:
    	def __init__(self,base_url,current_,total,per_page,page_numb)
    	"base_url:分页信息基础路径 当前页码  总的数据另  每页显示的数据量  显示页码数""
    
    self.base_url = base_url 
    self.current_page_num =current_page_num
    ....
    try : self.cur = int(curent)
    except Exce: self.current_ = 1 
    
    @property
    def start_num(self):start_num = (self....) return 
    @property
    def end_num(self):end_num = (self....) return 
    
    
    p_obj = PageN(....)
    all_data = models.Cu.ob.all()[p_obj.start_num]
    

    自定义分页,之后直接使用,功能要全

    view.py

    from nbapp import models
    
    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()
        per_page_counts = 10
        # 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
    
    
        '''
            <nav aria-label="Page navigation">
              <ul class="pagination">
                <li>
                  <a href="#" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                  </a>
                </闪电>
                <li><a href="#">1</a></闪电>
                <li><a href="#">2</a></闪电>
                <li><a href="#">3</a></闪电>
                <li><a href="#">4</a></闪电>
                <li><a href="#">5</a></闪电>
                <li>
                  <a href="#" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                  </a>
                </闪电>
              </ul>
            </nav>
        '''
    
        tab_html = ''
        tab_html += '<nav aria-label="Page navigation"><ul class="pagination">'
    
        #上一页
        if current_page_num == 1:
            previous_page = '<li disabled><a href="#" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></闪电>'
        else:
            previous_page = '<li><a href="{0}?page={1}" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></闪电>'.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 = '<li class="active"><a href="{0}?page={1}">{1}</a></闪电>'.format(base_url,i)
            else:
                one_tag = '<li><a href="{0}?page={1}">{1}</a></闪电>'.format(base_url, i)
            tab_html += one_tag
    
    
        #下一页
        if current_page_num == page_number_count:
            next_page = '<li disabled><a href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></闪电>'
        else:
            next_page = '<li><a href="{0}?page={1}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></闪电>'.format(base_url,current_page_num+1)
        tab_html+=next_page
        tab_html += '</ul></nav>'
    
        return tab_html,start_num,end_num
    
    
    view视图
    from nbapp import page
    def test(request):
        current_page_num = request.GET.get('page',1)
        per_page_counts = 10 #每页显示10条
        page_number = 5  #总共显示5个页码
    
    
    
        all_data = models.Customer.objects.all()
        total_count = all_data.count()
    
        ret_html,start_num,end_num = page.pagenation(request.path, current_page_num,total_count,per_page_counts,page_number)
    
        all_data = models.Customer.objects.all()[start_num:end_num]
    
        return render(request,'test.html',{'all_data':all_data,'ret_html':ret_html})
    
    
    

    cbv view

    form djaong.views i View 
    class AddCustomer(View):
     	def get(self,request):
    
    

    form .py

    import re
    from nbapp import models
    from django import forms
    from django.forms import widgets
    from django.core.exceptions import ValidationError
    class UserForm(forms.Form):
    	usernaem password r_password
    
    modelform把form相应的字段给继承了过来,不用一个个写了	username = forms.CharField()
    pwd =.... 这些  modelForm把form的东西包装了   内部生成form标签内部前端传过来form标签 检验 
    
    class CustomerModelForm(forms.ModelForm):
    	class Meta: #根据这个表中的自动生成form	不用自己在写  继承了
    		model = models.Customer
    		fields = '__all__'
    		 # exclude=['title',] #排除字段
    		labels = {‘title’:'书名'}
    	def clean_qq(self):
        	qq= self.cleaned_data.get('qq')
        	if '123' in qq:
        		raise ValidationError('QQ藜麦不能123')
        	else:
        		return qq 
        		
    	def clean(self):	校验多个字段
        	password = 
        	r_password = self.clean_data.get('')
        if p!= r_p:
        	self.add_error('r','两次错误')
        		
    

    xxx.py

    import os
    
    if __name__ == '__main__':
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "NBcrm.settings")
        import django
        django.setup()
        from nbapp import models
        import random
        l1 = []
        for i in range(1,101):
            obj = models.Customer(
                qq = ''.join([str(i) for i in random.choices(range(1,10),k=11)]),
                name = 'lihua'+ str(i),
                sex = random.choice(['male','female']),
                source = random.choice(['qq','referral','website']),
                course=random.choice(['LinuxL','PythonFullStack']),
    
            )
            l1.append(obj)
        models.Customer.objects.bulk_create(l1)
    

    modelform生成多选框

    page.py

    #自定义分页
    
    class PageNation:
        def __init__(self,base_url,current_page_num,total_counts,per_page_counts=10,page_number=5):
            '''
            :param base_url:   分页展示信息的基础路径
            :param current_page_num:  当前页页码
            :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
            try:
                self.current_page_num = int(self.current_page_num)
    
            except Exception:
                self.current_page_num = 1
    
            half_page_range = self.page_number // 2
            # 计算总页数
            self.page_number_count, a = divmod(self.total_counts, self.per_page_counts)
            if self.current_page_num < 1:
                self.current_page_num = 1
    
            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:
                    self.page_start = 1
                    self.page_end = page_number
                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
    
        #数据切片依据,起始位置
        @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 += '<nav aria-label="Page navigation"><ul class="pagination">'
    
            # 上一页
            if self.current_page_num == 1:
                previous_page = '<li disabled><a href="#" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></li>'
            else:
                previous_page = '<li><a href="{0}?page={1}" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></li>'.format(
                    self.base_url, self.current_page_num - 1)
            tab_html += previous_page
    
            for i in range(self.page_start, self.page_end + 1):
                if self.current_page_num == i:
    
                    one_tag = '<li class="active"><a href="{0}?page={1}">{1}</a></li>'.format(self.base_url, i)
                else:
                    one_tag = '<li><a href="{0}?page={1}">{1}</a></li>'.format(self.base_url, i)
                tab_html += one_tag
    
            # 下一页
            if self.current_page_num == self.page_number_count:
                next_page = '<li disabled><a href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'
            else:
                next_page = '<li><a href="{0}?page={1}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'.format(self.base_url, self.current_page_num + 1)
            tab_html += next_page
            tab_html += '</ul></nav>'
    
            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 += '<nav aria-label="Page navigation"><ul class="pagination">'
    
        #上一页
        if current_page_num == 1:
            previous_page = '<li disabled><a href="#" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></li>'
        else:
            previous_page = '<li><a href="{0}?page={1}" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></li>'.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 = '<li class="active"><a href="{0}?page={1}">{1}</a></li>'.format(base_url,i)
            else:
                one_tag = '<li><a href="{0}?page={1}">{1}</a></li>'.format(base_url, i)
            tab_html += one_tag
    
    
        #下一页
        if current_page_num == page_number_count:
            next_page = '<li disabled><a href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'
        else:
            next_page = '<li><a href="{0}?page={1}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'.format(base_url,current_page_num+1)
        tab_html+=next_page
        tab_html += '</ul></nav>'
    
        return tab_html,start_num,end_num
    

    下午

    models.Custome(request.post) 把form标签拿过来验证
    
    
    
    mutiselect() 	djaong不做处理
    
    modeform 里没写标签用的model里的vebname 
    
    modeform 生成标签的时候
    
    blank = True 和 null = True 
    
    form浏览器只认blank   数据库认null
    
    form_obj = form.CustomerModelform(instance= custome_obj) 老的数据 给了modelform
    
    认证的时候 (两个参数)(re.PO,in=cu_ob)
    
    不写验证是添加,没有instance时候
    
    两个一样
    action = '{{url 'customer' pk}}'    
    action = '' 这个页面 
    

    q查询高级玩法

    q = Q()

    q.children.append((condition,wd))

    就把字符串变成了关键字 filter(q)

    condition = condition += '__contains'

    多条查询

    q = Q() 
    q.children.append((condition,wd)) 
    q.children.append((‘qq_name__contains’,'小'))  添加了两条 默认是and的关系
    
    若为or   指定条件连接符
    再q.append之前添加  q.connector = 'or'
    

    保存所有条件 搜索必备

    1 加到标签里  init里传过来
    
    2 params = request.GET.dict()
    querydict     字典形式
    params.urlencode() condition=qq & contain= 姓名
    
    
    
    type(re.GET)  djaong.http.request.Q
    
    查看  from dj.ht.re import Qu
    
    deepcopy 可以修改  (request.GET)
    
    为True
    
    在page里
    循环里重新赋值  每一次 self.params[''] = i
    
  • 相关阅读:
    Python中使用pip安装库时提示:远程主机强迫关闭了一个现有的连接
    一个真正有能力的人
    一个程序员的自白(延迟满足)
    小规模团队如何“微服务”管理
    一个程序员的自白(无知之者)
    小型系统如何“微服务”开发
    TCP协议学习总结(下)
    TCP协议学习总结(中)
    TCP协议学习总结(上)
    一个程序员的自白(三十而立)
  • 原文地址:https://www.cnblogs.com/Doner/p/11003016.html
Copyright © 2011-2022 走看看