1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
| from django.shortcuts import render,HttpResponse,redirect from .models import Department,Employee,PrettyNum from django import forms from django.core.exceptions import ValidationError from django.core.validators import RegexValidator from django.utils.safestring import mark_safe from app01.utils.pagination import Pagination import copy # Create your views here. def department_list(request): member_list=Department.objects.all() return render(request,'dep.html',{'member':member_list})
def add_dep(request): if request.method=='GET': return render(request,'adddep.html') elif request.method=='POST': title=request.POST.get('aatitle') Department.objects.create(tittle=title) return redirect("/dep/list/")
def delete_dep(request): memid=request.GET.get('memid') Department.objects.filter(id=memid).delete() return redirect("/dep/list/") def edit_dep(request,memid): if request.method=='GET': nnmame=Department.objects.filter(id=memid).first() return render(request,'editdpt.html',{'nnmame':nnmame.tittle}) aatitle=request.POST.get('aatitle') Department.objects.filter(id=memid).update(tittle=aatitle) return redirect("/dep/list/")
def test(request): return render(request,'tt.html') def usr_list(request): # for i in range(300): # Employee.objects.create(name="maomao",password=123,age=50,create_time='2023-10-5',depart_id=5) qurylist=Employee.objects.all()
page_object=Pagination(request,qurylist)
context={ "qurylist":page_object.page_qurylist, "page_string":page_object.html(), } return render(request,'usr.html',context) # class MyForm(forms.Form): # usr=forms.CharField(widget=forms.EmailInput) # pwd=forms.CharField(widget=forms.PasswordInput) # age=forms.ImageField(widget=forms.NumberInput) # class MyForm(forms.Form): # class Meta: # model=Employee # fields=['name','password','age'] def add_usr(request): context={ 'gender_choice':Employee.gender_choices, 'depart_list':Department.objects.all()
} # form=MyForm() return render(request,'add_usr.html',context) # return render(request,'add_usr_fro.html',{'form':form})
class MyForm(forms.ModelForm): # name=forms.CharField(min_length=3,label="用户名") # usr=forms.CharField(widget=forms.EmailInput) # pwd=forms.CharField(widget=forms.PasswordInput) # age=forms.ImageField(widget=forms.NumberInput) class Meta: model=Employee fields=['name','password','age','account','create_time','depart','gender'] widgets={ "name":forms.TextInput(attrs={"class":"form-control"}), "age":forms.TextInput(attrs={"class":"form-control"}), "create-time":forms.TextInput(attrs={"type":"data"}) } # def __init__(self,*args, **kwargs): # super().__init__(*args,**kwargs) # # for name,field in self.fields.items(): # field.Widget.attrs={"class":"form-control","placeholder":field.label}
def add_usr_form(request): if request.method == "GET": form=MyForm() return render(request,'add_usr_fro.html',{'form':form}) # post 校验 form=MyForm(data=request.POST) if form.is_valid(): form.save() return redirect('/usr/list/') else: return render(request,'add_usr_fro.html',{"form":form}) def usr_edit(request,nid): #获取默认值 row_object=Employee.objects.filter(id=nid).first() if request.method =='GET': form =MyForm(instance=row_object) return render(request,'usr_edit.html',{'form':form}) #post form=MyForm(data=request.POST,instance=row_object) if form.is_valid(): form.save() return redirect('/usr/list/') return render(request,'usr_edit.html',{'form':form}) def usr_delete(request,nid): Employee.objects.filter(id=nid).delete() return redirect('/usr/list/') class PrettyForm(forms.ModelForm): # 验证方法一 # mobile=forms.CharField( # label="手机号", # validators=[RegexValidator(r'^1[3-9]\d{9}$','手机号格式错误')] # ) class Meta: model=PrettyNum fields=['mobile','price','level','status'] # fields="__all__" # exclude=['level'] widgets={ "mobile":forms.TextInput(attrs={"class":"form-control"}), "price":forms.TextInput(attrs={"class":"form-control"}), "level":forms.TextInput(attrs={"class":"form-control"}), "status":forms.TextInput(attrs={"class":"form-control"}), }
# 验证方法二 def clean_mobile(self): txt_mobile=self.cleaned_data["mobile"] exists=PrettyNum.objects.filter(mobile=txt_mobile).exists()
if len(txt_mobile)!=11: #验证不通过 raise ValidationError("格式错误") if exists: #验证不通过 raise ValidationError("手机号已存在") return txt_mobile def pretty_list(request): ##搜索 # -------------------- # 1.PrettyNum.objects.filter{mobile='13906135233',"id":123} # 2.data_dict={"mobile":"13906135233","id":123} # PrettyNum.objects.filter(**data_dict) # -------------------------------- # PrettyNum.objects.filter(id=12) 等于12 # PrettyNum.objects.filter(id__gt=12) 大于12 # PrettyNum.objects.filter(id__gte=12) 大于等于12 # PrettyNum.objects.filter(id__lt=12) 小于12 # PrettyNum.objects.filter(id__lte=12) 小于等于12
# data_dict={"id__lte":12} # # ----------------------------------- # PrettyNum.objects.filter(mobile='233') 等于 # PrettyNum.objects.filter(mobile__startswitch="139") 筛选出以139开头 # PrettyNum.objects.filter(mobile__endswitch="233") 筛选出以233结尾 # PrettyNum.objects.filter(mobile__contains="5233") 筛选出包含5233 # use # data_dict={"mobile__contains":"233"} # PrettyNum.objects.filter(**data_dict) # ---------------------------------------- # test # for i in range(300): # PrettyNum.objects.create(mobile="13906135899",price=10,level=1,status=1) # print(request.GET) # request.GET.setlist('xx',11) # print(request.GET.urlencode())
#q=123&page=2
# get_object=copy.deepcopy(request.GET) # get_object._mutable=True # get_object.setlist('page',[11]) # print(get_object.urlencode()) qury_dict=copy.deepcopy(request.GET) qury_dict._mutable=True qury_dict.setlist('page',[11]) print(qury_dict.urlencode())
data_dict={} value=request.GET.get('q','') if value: data_dict["mobile__contains"] = value
qurylist=PrettyNum.objects.filter(**data_dict).order_by("-level") page_object= Pagination(request,qurylist) page_qurylist=page_object.page_qurylist page_string=page_object.html() context={'qurylist':page_qurylist, "search_data":value, "page_string":page_string} # res=PrettyNum.objects.filter(**data_dict) # print(res)
# if request.method=='GET': # ****qurylist=PrettyNum.objects.filter(**data_dict).order_by("-level")[start:end] #select * from 表 order by level desc # qurylist=PrettyNum.objects.all().order_by("-level") #分页 # qurylist=PrettyNum.objects.all() # qurylist=PrettyNum.objects.filter(id=4)[0:10] # qurylist=PrettyNum.objects.all()[0:10] # qurylist=PrettyNum.objects.all()[10:20] # qurylist=PrettyNum.objects.all()[20:30] # page=int(request.GET.get('page',1)) # pageSize=10 # start=(page-1)*pageSize # end=page*pageSize
# qurylist=PrettyNum.objects.filter(**data_dict).order_by("-level")[page_object.start:page_object.end] #总数据条数 # total_count=PrettyNum.objects.filter(**data_dict).order_by("-level").count() # #总页码 # total_page_count,div=divmod(total_count,pageSize) # if div: # total_page_count+=1
# 计算出,显示当前页的前5页、后5页 # plus=5 # if total_page_count<=2*plus+1: # #数据库中的数据比较少,都没有达到11页 # start_page=1 # end_page=total_page_count # else: # #数据库中的数据比较多 >11页
# #当前页<5时(极小值) # if page<=plus: # start_page=1 # end_page=2*plus+1 # else: # #当前页>5 # #当前页+5>总页面 # if (page+plus)>total_page_count: # start_page=total_page_count-2*plus # end_page=total_page_count # else: # start_page=page-plus # end_page=page+plus # #页码 # page_str_list=[] # #首页 # page_str_list.append('<li><a href="?page={}">首页</a></li>'.format(1)) # #上一页 # if page>1: # prev='<li><a href="?page={}">上一页</a></li>'.format(page-1) # else: # prev='<li><a href="?page={}">上一页</a></li>'.format(1) # page_str_list.append(prev) # for i in range(start_page,end_page+1): # if i==page: # ele='<li class="active"><a href="?page={}">{}</a></li>'.format(i,i) # else: # ele='<li><a href="?page={}">{}</a></li>'.format(i,i) # page_str_list.append(ele) # #下一页 # if page<total_page_count: # prev='<li><a href="?page={}">下一页</a></li>'.format(page+1) # else: # prev='<li><a href="?page={}">下一页</a></li>'.format(total_page_count) # page_str_list.append(prev) # #尾页 # page_str_list.append('<li><a href="?page={}">尾页</a></li>'.format(total_page_count)) # search_string="""" # <form class="navbar-form navbar-left" method="GET"> # <div class="form-group"> # <input type="text" class="form-control" placeholder="Search" name="page"> # </div> # <button type="submit" class="btn btn-default">Submit</button> # </form> # """ # page_str_list.append(search_string) # page_string =mark_safe("".join(page_str_list)) return render(request,'pretty_list.html',context) def pretty_add(request): if request.method=='GET': form=PrettyForm() return render(request,'pretty_add.html',{'form':form}) #post校验 form=PrettyForm(data=request.POST) if form.is_valid(): form.save() return redirect('/pretty/list/') else: return render(request,'pretty_add.html',{"form":form}) class PrettyEditModelForm(forms.ModelForm): # 验证方法一 mobile=forms.CharField( label="手机号", # disabled=True, validators=[RegexValidator(r'^1[3-9]\d{9}$','手机号格式错误')], ) ## mobile=forms.CharField(disabled=True,label="手机号")
class Meta: model=PrettyNum fields=['mobile','price','level','status'] def clean_mobile(self): # print(self.instance.pk) txt_mobile=self.cleaned_data["mobile"] exists=PrettyNum.objects.exclude(id=self.instance.pk).filter(mobile=txt_mobile).exists() # if len(txt_mobile)!=11: # # 验证不通过 # raise ValidationError("格式错误") if exists: #验证不通过 raise ValidationError("手机号已存在") return txt_mobile def pertty_edit(request,nid): row_object=PrettyNum.objects.filter(id=nid).first() if request.method=='GET': form=PrettyEditModelForm(instance=row_object) return render(request,'pretty_edit.html',{'form':form}) #post form=PrettyEditModelForm(data=request.POST,instance=row_object) if form.is_valid(): form.save() return redirect('/pretty/list/') else: return render(request,'pretty_edit.html',{'form':form}) def pertty_delete(request,nid): PrettyNum.objects.filter(id=nid).delete() return redirect('/pretty/list/')
|