今天分享一个博客适合刚准备学习Pythonweb开发的小伙伴。
系统涉及功能:
1:运用django+mysql实现了数据的增删改查以及搜索。
2:实现了对前端语言和前端bootstrap库的基本运用。
3:实现了自定义分页的功能以及指定页码跳转。
项目涉及技术:
Python的django框架,ORM数据*(库)架构,前端Web语言,python语言。
运行环境:
Django3.8,数据库是8.7,编译器是Pyharm专业版,网页效果展示小智双核浏览器。
代码展示:
只展示view.py文件,其他就不过多展示,因为本小项目的逻辑都在view.py类文件实现。
from django.shortcuts import render,redirect
from app01 import models
from django import forms
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
from django.utils.safestring import mark_safe
from app01.utils.pagination import Pagination
# Create your views here.
def depart_list(request):
query_set=models.Department.objects.all()
page_object = Pagination(request, query_set, page_size=5)
context = {
"query_set": page_object.page_queryset,
"page_string": page_object.html()
}
return render(request, "depart_list.html", context)
def depart_add(request):
if request.method == "GET":
return render(request,"depart_add.html")
title=request.POST.get("title")
models.Department.objects.create(title=title)
return redirect("/depart/list/")
def depart_delete(request):
nid=request.GET.get("id")
models.Department.objects.filter(id=nid).delete()
return redirect("/depart/list/")
def depart_modify(request,id):
if request.method=="GET":
row_object=models.Department.objects.filter(id=id).first()
return render(request, "depart_modify.html", {"row_object":row_object})
title=request.POST.get("title")
models.Department.objects.filter(id=id).update(title=title)
return redirect("/depart/list/")
def user_list(request):
queryset=models.UserInfo.objects.all()
page_object=Pagination(request,queryset,page_size=2)
context={
"queryset":page_object.page_queryset,
"page_string":page_object.html()
}
# for obj in queryset:
# print(obj.id,obj.name,obj.account,obj.create_time.strftime("%Y-%m-%d"),obj.get_gender_display(),obj.depart.title)
# depart_id=表中数据int depart==所关联表中对应的对象 depart.title
# print(obj.depart.title)
return render(request, "user_list.html", context)
def user_add(request):
if request.method=="GET":
context={
"gender_choice":models.UserInfo.gender_choices,
"depart_list":models.Department.objects.all()
}
return render(request,"user_add.html",context)
user=request.POST.get("user")
pwd=request.POST.get("pwd")
age=request.POST.get("age")
account=request.POST.get("account")
gender=request.POST.get("gd")
ctime=request.POST.get("ctime")
dapart_id=request.POST.get("dp")
models.UserInfo.objects.create(name=user,password=pwd,age=age,account=account,gender=gender,create_time=ctime,depart_id=dapart_id)
return redirect("/user/list/")
class UserModelForm(forms.ModelForm):
# name=forms.CharField(min_length=3,label="用户名")
# password=forms.CharField(label="密码",validators="可填写正则表达式来确定规范")
class Meta:
model=models.UserInfo
fields=["name","password","age","account","create_time","gender","depart"]
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
for name,field in self.fields.items():
# 不添加该密码输入框样式
# if name=="password":
# continue
field.widget.attrs={"class":"form-control","placeholder":field.label}
def user_model_form_add(request):
if request.method=="GET":
form=UserModelForm()
return render(request,"user_model_add.html",{"form":form})
form =UserModelForm(data=request.POST)
if form.is_valid():
form.save()
return redirect("/user/list/")
return render(request,"user_model_add.html",{"form":form})
def user_modify(request,id):
if request.method=="GET":
row_object=models.UserInfo.objects.filter(id=id).first()
form=UserModelForm(instance=row_object)
return render(request,"user_modify.html",{"form":form})
row_object=models.UserInfo.objects.filter(id=id).first()
form=UserModelForm(data=request.POST,instance=row_object)
if form.is_valid():
form.save()
return redirect("/user/list/")
return render(request,"user_modify.html",{"form":form})
def user_delete(request,id):
models.UserInfo.objects.filter(id=id).delete()
return redirect("/user/list")
def phone_list(request):
# for i in range(100):
# models.PrettyNum.objects.create(mobile="16427309716",price=10,level=1,status=1)
data_dict={}
value = request.GET.get('q',"")
if value:
data_dict["mobile__contains"]=value
queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")
page_object=Pagination(request,queryset)
# page_queryset=page_object.page_queryset
# page_string=page_object.html()
context={
"value":value,
"queryset":page_object.page_queryset, # 分页完的数据
"page_string": page_object.html() # 页码
}
"""
# 1.根据用户想要访问的页码,计算起止位置
page=int(request.GET.get('page',1))
page_size=10
start=(page-1)*page_size
end=page*page_size
total_count= models.PrettyNum.objects.filter(**data_dict).order_by("-level").count()
"""
# 总页码
# total_page,div=divmod(total_count,page_size)
# if div:
# total_page+=1
# queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")[page_object.start:page_object.end]
# for obj in queryset:
# print(obj.id, obj.mobile, obj.price,obj.get_level_display(),obj.get_status_display())
# depart_id=表中数据int depart==所关联表中对应的对象 depart.title
# print(obj.depart.title)
return render(request,"phone_list.html",context)
class PhoneModelForm(forms.ModelForm):
# models=forms.CharField(disabled=True,label="手机号") 设定该字段内容不可改
mobile=forms.CharField(
label="手机号",
validators=[RegexValidator(r'^1\d{10}$',"手机号格式错误")]
)
# name=forms.CharField(min_length=3,label="用户名")
# password=forms.CharField(label="密码",validators="可填写正则表达式来确定规范")
class Meta:
model=models.PrettyNum
# fields="__all__" 展示所有字段
# exclude=["level"] 排除指定字段
fields=["mobile","price","level","status"]
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
for name,field in self.fields.items():
# 不添加该密码输入框样式
# if name=="password":
# continue
field.widget.attrs={"class":"form-control","placeholder":field.label}
def clean_mobile(self):
txt_mobile=self.cleaned_data["mobile"]
exists=models.PrettyNum.objects.filter(mobile=txt_mobile).exists()
if exists:
raise ValidationError("手机号已存在")
if len(txt_mobile)!=11:
raise ValidationError("格式错误")
return txt_mobile
class PhoneModelForm(forms.ModelForm):
# models=forms.CharField(disabled=True,label="手机号") 设定该字段内容不可改
mobile=forms.CharField(
label="手机号",
validators=[RegexValidator(r'^1\d{10}$',"手机号格式错误")]
)
# name=forms.CharField(min_length=3,label="用户名")
# password=forms.CharField(label="密码",validators="可填写正则表达式来确定规范")
class Meta:
model=models.PrettyNum
# fields="__all__" 展示所有字段
# exclude=["level"] 排除指定字段
fields=["mobile","price","level","status"]
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
for name,field in self.fields.items():
# 不添加该密码输入框样式
# if name=="password":
# continue
field.widget.attrs={"class":"form-control","placeholder":field.label}
def clean_mobile(self):
txt_mobile=self.cleaned_data["mobile"]
exists=models.PrettyNum.objects.filter(mobile=txt_mobile).exists()
if exists:
raise ValidationError("手机号已存在")
if len(txt_mobile)!=11:
raise ValidationError("格式错误")
return txt_mobile
# 编辑ModelForm
class PhoneEditModelForm(forms.ModelForm):
# models=forms.CharField(disabled=True,label="手机号") 设定该字段内容不可改
mobile=forms.CharField(
label="手机号",
validators=[RegexValidator(r'^1\d{10}$',"手机号格式错误")]
)
# name=forms.CharField(min_length=3,label="用户名")
# password=forms.CharField(label="密码",validators="可填写正则表达式来确定规范")
class Meta:
model=models.PrettyNum
# fields="__all__" 展示所有字段
# exclude=["level"] 排除指定字段
fields=["mobile","price","level","status"]
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
for name,field in self.fields.items():
# 不添加该密码输入框样式
# if name=="password":
# continue
field.widget.attrs={"class":"form-control","placeholder":field.label}
def clean_mobile(self):
txt_mobile=self.cleaned_data["mobile"]
exists1=models.PrettyNum.objects.exclude(id=self.instance.pk).filter(mobile=txt_mobile).exists()
if exists1:
raise ValidationError("手机号编辑重复")
if len(txt_mobile)!=11:
raise ValidationError("格式错误")
return txt_mobile
def phone_add(request):
if request.method=="GET":
form=PhoneModelForm()
return render(request,"phone_add.html",{"form":form})
form = PhoneModelForm(data=request.POST)
if form.is_valid():
form.save()
return redirect("/phone/list/")
return render(request,"phone_add.html",{"form":form})
def phone_modify(request,id):
if request.method=="GET":
row_object=models.PrettyNum.objects.filter(id=id).first()
form=PhoneEditModelForm(instance=row_object)
return render(request,"phone_modify.html",{"form":form})
row_object=models.PrettyNum.objects.filter(id=id).first()
form=PhoneEditModelForm(data=request.POST,instance=row_object)
if form.is_valid():
form.save()
return redirect("/phone/list/")
return render(request,"phone_modify.html",{"form":form})
def phone_delete(request,id):
models.PrettyNum.objects.filter(id=id).delete()
return redirect("/phone/list/")
部分运行截图:
文章来源:https://www.toymoban.com/news/detail-437416.html
如果有需要源码文件的同学可以私信我! 文章来源地址https://www.toymoban.com/news/detail-437416.html
到了这里,关于python+django+sql实现简单的数据信息管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!