您现在的位置是:网站首页> 编程资料编程资料
Django超详细讲解图书管理系统的实现_python_
2023-05-26
374人已围观
简介 Django超详细讲解图书管理系统的实现_python_
项目使用python开发,采用Django框架,数据库采用MySQL,根据用户人员的不同分成两套系统,分别是学生系统和管理员系统,功能模块具体分成四个,分别是用户管理模块、图书管理模块、数据管理模块、前端模块。
1、用户管理模块
用户管理模块实现的功能包括用户注册(分为学生注册和管理员注册)、用户信息修改、用户登录和判定
用户注册和登录

views.py中用户注册及登陆判定代码段
def login(request):#登录 return render(request, 'login.html') def student_register(request): # 学生注册 name = request.POST.get("student_name") # 获取学生输入的姓名 id = request.POST.get("student_id") # 获取学生输入的学号 major = request.POST.get("student_major") # 获取学生输入的学院 email = request.POST.get("student_email") # 获取学生输入的邮箱 telephone = request.POST.get("student_telephone") password = request.POST.get("student_password") result1 = User.objects.filter(account=telephone) # 在用户表中搜索该用户名的记录 result2 = Student.objects.filter(student_id=id) # 在学生表中搜索该学号的记录 context = {} if len(result1) == 1: # 判断该账户是否存在(即判断是否注册过),如果后台存在记录,则返回相应的提示语句 context["info"] = "该账户已注册!!!" context["status"] = 0 #零表示注册失败 return render(request, 'login.html', context=context) else: #该账户是新用户 if len(result2) == 1:#判断该学号是否有学生已使用 context["info"] = "该学号已占用!!!" context["status"] = 4 return render(request, 'login.html', context=context) else: User.objects.create(account=telephone, user_password=password,user_identity='学生')#用create为user表添加一条记录 Student.objects.create(student_name=name,student_id=id,student_major=major,student_tel=telephone,student_email=email)#用create为student表添加一条记录 context["info"] = "注册成功!" context["status"] = 1 #1表示注册成功 return render(request, 'login.html', context=context) def manager_register(request): # 管理员注册 name = request.POST.get("manager_name") # 获取管理员输入的姓名 id = request.POST.get("manager_id") # 获取管理员输入的工号 stack = request.POST.get("manager_stack") # 获取管理员输入的书库 email = request.POST.get("manager_email") # 获取管理员输入的邮箱 telephone = request.POST.get("manager_telephone") password = request.POST.get("manager_password") result1 = User.objects.filter(account=telephone) # 在用户表中搜索该用户名的记录 result2 = Manager.objects.filter(manager_id=id) # 在管理员表中搜索该工号的使用记录 context = {} if len(result1) == 1: # 判断该账户是否存在(即判断是否注册过),如果后台存在记录,则返回相应的提示语句 context["info"] = "该账户已注册!!!" context["status"] = 0 #零表示注册失败 return render(request, 'login.html', context=context) else: #该账户是新用户 if len(result2) == 1:#判断该工号号是否有管理员已使用 context["info"] = "该工号已占用!!!" context["status"] = 5 return render(request, 'login.html', context=context) else: User.objects.create(account=telephone, user_password=password,user_identity='管理员')#用create为user表添加一条记录 Manager.objects.create(manager_name=name, manager_id=id, manager_stack=stack, manager_tel=telephone,manager_email=email)#用create为manager表添加一条记录 context["info"] = "注册成功!" context["status"] = 1 #1表示注册成功 return render(request, 'login.html', context=context) def login_judge(request):#登入判定 global account ,global_sname,global_mname #定义全局变量account,存储该用户的账户,global_sname保存一下该学生的姓名,global_mname保存一下该学生的姓名 account = request.POST.get("telephone")#获取前端输入的账户(手机号) user_password = request.POST.get("password") result1 = User.objects.filter(account=account)#在user表里检索是否存在该账户 if len(result1) == 1: # 判断后台是否存在该用户,有则进一步判断密码是否正确 password = result1[0].user_password # 获取后台的密码 identity = result1[0].user_identity # 获取该账户的身份信息 if user_password == password: # 将用户输入的密码和后台密码进行比对,如何正确,判断该账户身份 if identity == '学生': result2 = Student.objects.filter(student_tel=account) global_sname = result2[0].student_name # 用全局变量保存一下该学生的姓名 context={ "name":result2[0].student_name, "id":result2[0].student_id, "major":result2[0].student_major, "telephone":result2[0].student_tel, "email":result2[0].student_email, } return render(request, 'student/student_information.html',context) # 跳转到学生主页界面 else: result = Manager.objects.filter(manager_tel=account) # account为全局变量 global_mname = result[0].manager_name # 用全局变量保存一下该管理员的姓名 context = { "name": result[0].manager_name, "id": result[0].manager_id, "stack": result[0].manager_stack, "telephone": result[0].manager_tel, "email": result[0].manager_email, } return render(request, 'manager/manager_information.html',context) # 跳转到管理员主页界面 else: # 如果不一致则返回相应提示语句 context = { "info": "密码错误!!!", "status": 2 } return render(request, 'login.html', context=context) # 密码错误回到登入界面 else: # 如果不存在该用户则返回相应的提示语句 context = { "info": "该账户不存在!!!", "status": 3 } return render(request, 'login.html', context=context) # 账户不存在则继续回到登入界面用户信息管理

views.py中用户信息管理代码段
def student_information(request):#个人信息 if request.method == "GET": #此部分是当每次点击侧边导航栏的“个人信息”选项时,都重新显示该用户的个人资料 result = Student.objects.filter(student_tel=account) #account为全局变量 context = { "name": result[0].student_name, "id": result[0].student_id, "major": result[0].student_major, "telephone": result[0].student_tel, "email": result[0].student_email, } return render(request, 'student/student_information.html', context)#将该用户的个人信息再次传到前端页面 else: #在student_information.html页面的第44行中通过post方式的“保存”按钮跳转到此处,即完成更新数据操作(保存) email = request.POST.get("email") # 获取邮箱 Student.objects.filter(student_tel=account).update(student_email=email)#更新数据 result = Student.objects.filter(student_tel=account) # account为全局变量 此处再次传值到前端 context = { "name": result[0].student_name, "id": result[0].student_id, "major": result[0].student_major, "telephone": result[0].student_tel, "email": result[0].student_email, } return render(request, 'student/student_information.html', context) # 将该用户的个人信息再次传到前端页面 def manager_information(request):#个人信息 if request.method == "GET": #此部分是当每次点击侧边导航栏的“个人信息”选项时,都重新显示该管理员的个人资料 result = Manager.objects.filter(manager_tel=account) #account为全局变量 context = { "name": result[0].manager_name, "id": result[0].manager_id, "stack": result[0].manager_stack, "telephone": result[0].manager_tel, "email": result[0].manager_email, } return render(request, 'manager/manager_information.html', context)#将该用户的个人信息再次传到前端页面 else: #在manager_information.html页面的第44行中通过post方式的“保存”按钮跳转到此处,即完成更新数据操作(保存) stack = request.POST.get("stack") # 获取书库信息 email = request.POST.get("email") # 获取邮箱 Manager.objects.filter(manager_tel=account).update(manager_email=email,manager_stack=stack)#更新数据 result = Manager.objects.filter(manager_tel=account) # account为全局变量 此处再次传值到前端 context = { "name": result[0].manager_name, "id": result[0].manager_id, "stack": result[0].manager_stack, "telephone": result[0].manager_tel, "email": result[0].manager_email, } return render(request, 'manager/manager_information.html', context) # 将该用户的个人信息再次传到前端页面用户密码修改

views.py中用户密码修改代码段
def change_password(request):#修改密码 result = User.objects.filter(account=account).first() password = result.user_password if request.method == "GET": #此部分是当每次点击侧边导航栏的“修改密码”选项时,显示该界面 return render(request,'student/change_password.html',context={"password":password,"name":global_sname}) else:#此部分是在change_password.html页面中点击保存按钮时完成修改密码的操作 oldPassword = request.POST.get("oldPassword") newPassword = request.POST.get("newPassword") reNewPassword = request.POST.get("reNewPassword")#以下是先判断输入的旧密码是否正确,并且两次输入的密码是否一致且都不为空 if password == oldPassword and newPassword == reNewPassword and newPassword and reNewPassword: User.objects.filter(account=account).update(user_password = newPassword)#更新该用户的密码 password = newPassword return render(request, 'student/change_password.html', context={"password": password, "name": global_sname}) def change_manager_password(request):#修改管理员的密码 result = User.objects.filter(account=account).first() password = result.user_password if request.method == "GET":#此部分是当每次点击侧边导航栏的“修改密码”选项时,显示该界面 return render(request,'manager/change_manager_password.html',context={"password":password,"name":global_mname}) else:#此部分是在change_manager_password.html页面中点击保存按钮时完成修改密码的操作 oldPassword = request.POST.get("oldPassword") newPassword = request.POST.get("newPassword") reNewPassword = request.POST.get("reNewPassword")#以下是先判断输入的旧密码是否正确,并且两次输入的密码是否一致且都不为空 if password == oldPassword and newPassword == reNewPassword and newPassword and reNewPassword: User.objects.filter(account=account).update(user_password = newPassword)#更新该用户的密码 password = newPassword return render(request, 'manager/change_manager_password.html', context={"password": password, "name": global_mname})2、图书管理模块
图书馆里模块实现的功能与我们日常图书馆的借阅系统相似,学生端包括书籍查询、书籍借阅、书记归还;管理员端包括书籍采购、书籍信息修改等更多扩展功能
书籍查询及借阅归还,可选择按书籍名或类型查找

views代码段
def search_book(request):#查找书籍 if request.method == "GET":#此部分是当用户每次点击侧边导航栏的“查找书籍”选项时,都要显示出所有书籍资料 books = Book.objects.all() types = Type.objects.all() return render(request, 'student/search_book.html',context={"books": books,"types":types,"name":global_sname }) # 向前端传递所有查找到的书籍信息的集合 else:#student/search_book.html页面的第56行中通过post方式的“搜索”按钮跳转到此处,即完成搜索操作 book_name = request.POST.get("book_name") type_id = request.POST.get("type_id") types = Type.objects.all() if book_name:#如果书名非空,则按书名查找 book_result = Book.objects.filter(book_name=book_name) if book_result:#如果找到的结果集非空,则输出 return render(request,'student/search_book.html',context={"books":book_result,"types":types,"name":global_sname}) else:#若搜索的结果集为0,那么输出未找到该本书! book_result = Book.objects.all() return render(request, 'student/search_book.html',context={"books": book_result, "types": types, "name
相关内容
- 关于pygame自定义窗口创建及相关操作指南_python_
- 教你使用Python的pygame模块实现拼图游戏_python_
- Python海象运算符的用法教程_python_
- pygame学习笔记之设置字体及显示中文_python_
- 详解Python中matplotlib模块的绘图方式_python_
- pytest多文件执行顺序控制详解_python_
- Python+matplotlib绘制多子图的方法详解_python_
- Python服务器创建虚拟环境跑代码_python_
- python基础篇之pandas常用基本函数汇总_python_
- PyTorch中apex安装方式和避免踩坑_python_
点击排行
本栏推荐
