当前位置:   article > 正文

基于Django开发的学生信息管理系统

django智慧星学生管理系统

来源:http://nxw.so/584pM



一、 功能

  1. 实现对学生对个人信息的增删查改

  2. 实现后台对所有学生信息的操作

二、开发工具

Windows + Pycharm + Mysql + Django

三、代码实现

1. model

  1. from django.db import models
  2. # Create your models here.
  3. # 课程表
  4. class CourseModel(models.Model):
  5. cour_id = models.CharField(max_length=15, verbose_name='学生ID')
  6. course = models.CharField(max_length=30, verbose_name='课程')
  7. grade = models.IntegerField(default=60, verbose_name='分数')
  8. class Meta():
  9. db_table = 'course'
  10. def __str__(self):
  11. return '学生Id:课程:分数:'.format(self.cour_id, self.course, self.grade)
  12. # 学生信息表
  13. class StudentInformationModel(models.Model):
  14. stu_id = models.CharField(max_length=15, verbose_name='学生ID')
  15. stu_name = models.CharField(max_length=30, verbose_name='学生姓名')
  16. stu_phone = models.CharField(max_length=20, verbose_name='学生电话')
  17. str_addr = models.TextField(verbose_name='学生地址')
  18. stu_faculty = models.CharField(max_length=20, verbose_name='院系')
  19. stu_major = models.CharField(max_length=30, verbose_name='专业')
  20. # 取消外键(外键是可用的)
  21. # stu_course = models.ForeignKey('CourseModel', on_delete=True)
  22. class Meta():
  23. db_table = 'studentinformation'
  24. # 学生用户名密码表
  25. class StudentModel(models.Model):
  26. stu_id = models.CharField(max_length=15, verbose_name='学生ID')
  27. username = models.CharField(max_length=10, verbose_name='用户名')
  28. password = models.CharField(max_length=10, verbose_name='密码')
  29. class Meta():
  30.         db_table = 'student'

2. urls

  1. from django.urls import path
  2. from studentManagement import views
  3. app_name = 'studentManager'
  4. urlpatterns = [
  5. path('index/', views.index, name='index'),
  6. path('login/', views.login, name='login'),
  7. path('logout/', views.logout, name='logout'),
  8. path('add/', views.add, name='add'),
  9. path('select/', views.select, name='select'),
  10. path('delete/', views.delete, name='delete'),
  11. path('update/', views.update, name='update')
  12. ]

3.  views

  1. from django.shortcuts import render, HttpResponse, redirect, reverse
  2. from .models import StudentModel, StudentInformationModel, CourseModel
  3. # Create your views here.
  4. # 主界面
  5. def index(request):
  6. context = {
  7. 'status': '未登录状态'
  8. }
  9. return render(request, 'studentManage/index.html', context)
  10. # 登录界面
  11. def login(request):
  12. if request.method == "POST":
  13. id = request.POST.get('id')
  14. username = request.POST.get('username')
  15. password = request.POST.get('password')
  16. if not all([id, username, password]):
  17. return HttpResponse('参数不全')
  18. else:
  19. student = StudentModel.objects.filter(username=username, password=password)
  20. if len(student):
  21. # request.session['username'] = username
  22. # 用以下方法,将用户的信息存放到session中,session在中间件中是默认启用的
  23. request.session['user'] = {
  24. 'id': id,
  25. 'username': username,
  26. 'password': password
  27. }
  28. context = {
  29. 'status': username,
  30. 'aa': '已登录',
  31. 'lenght': 1
  32. }
  33. return render(request, 'studentManage/index.html', context)
  34. else:
  35. context = {
  36. 'aa': '用户名密码错误'
  37. }
  38. return render(request, 'studentManage/login.html', context)
  39. else:
  40. context = {
  41. 'status': '未登录状态',
  42. 'length': 0
  43. }
  44. return render(request, 'studentManage/login.html', context)
  45. # 退出界面
  46. def logout(request):
  47. # 注销掉用户,从删除session中保存的信息
  48. del request.session['user']
  49. return render(request, 'studentManage/index.html')
  50. # 增加数据 增加只能root用户或者管理员才能操作
  51. def add(request):
  52. if request.method == "POST":
  53. root_information = request.session['user']
  54. id = root_information['id']
  55. root_id = StudentModel.objects.get(pk=1).stu_id
  56. if id == root_id:
  57. stu_id = request.POST.get('stu_id')
  58. stu_name = request.POST.get('stu_name')
  59. if not all([stu_id, stu_name]):
  60. context = {
  61. 'msg': '学号和名字有遗漏',
  62. }
  63. return render(request, 'studentManage/add.html', context)
  64. stu_phone = request.POST.get('stu_phone')
  65. stu_addr = request.POST.get('str_addr')
  66. stu_faculty = request.POST.get('stu_faculty')
  67. stu_major = request.POST.get('stu_major')
  68. stu_data = StudentInformationModel()
  69. stu_data.stu_id = stu_id
  70. stu_data.stu_name = stu_name
  71. stu_data.stu_phone = stu_phone
  72. stu_data.str_addr = stu_addr
  73. stu_data.stu_faculty = stu_faculty
  74. stu_data.stu_major = stu_major
  75. stu_data.save()
  76. context = {
  77. 'sucess': '增加成功',
  78. }
  79. return render(request, 'studentManage/add.html', context)
  80. else:
  81. context = {
  82. 'error': '只用root用户和管理员才能操作'
  83. }
  84. return render(request, 'studentManage/add.html', context)
  85. else:
  86. return render(request, 'studentManage/add.html')
  87. # 查询
  88. def select(request):
  89. if request.method == "POST":
  90. id = request.POST.get('stu_id')
  91. stu_data = StudentInformationModel.objects.get(stu_id=id)
  92. stu_id = stu_data.stu_id
  93. stu_name = stu_data.stu_name
  94. stu_phone = stu_data.stu_phone
  95. str_addr = stu_data.str_addr
  96. stu_faculty = stu_data.stu_faculty
  97. stu_major = stu_data.stu_major
  98. stu_course = CourseModel.objects.filter(cour_id=id)
  99. dct = {}
  100. for stu in stu_course:
  101. dct[stu.course] = stu.grade
  102. context = {
  103. 'stu_id': stu_id,
  104. 'stu_name': stu_name,
  105. 'stu_phone': stu_phone,
  106. 'str_addr': str_addr,
  107. 'stu_faculty': stu_faculty,
  108. 'stu_major': stu_major,
  109. 'course_data': dct,
  110. 'msg': True
  111. }
  112. return render(request, 'studentManage/select.html', context)
  113. else:
  114. root_information = request.session['user']
  115. id = root_information['id']
  116. context = {
  117. 'msg': False,
  118. 'id': id
  119. }
  120. return render(request, 'studentManage/select.html', context)
  121. # 删除
  122. def delete(request):
  123. if request.method == "POST":
  124. id = request.POST.get('id')
  125. StudentInformationModel.objects.filter(stu_id=id).delete()
  126. context = {
  127. 'msg': '成功删除'
  128. }
  129. return render(request, 'studentManage/delete.html', context)
  130. else:
  131. root_information = request.session['user']
  132. id = root_information['id']
  133. context = {
  134. 'id': id
  135. }
  136. return render(request, 'studentManage/delete.html', context)
  137. # 修改
  138. def update(request):
  139. user_information = request.session['user']
  140. id = user_information['id']
  141. stu_data = StudentInformationModel.objects.get(stu_id=id)
  142. stu_id = stu_data.stu_id
  143. stu_name = stu_data.stu_name
  144. stu_phone = stu_data.stu_phone
  145. stu_addr = stu_data.str_addr
  146. stu_faculty = stu_data.stu_faculty
  147. stu_major = stu_data.stu_major
  148. context = {
  149. 'stu_id': stu_id,
  150. 'stu_name': stu_name,
  151. 'stu_phone': stu_phone,
  152. 'stu_addr': stu_addr,
  153. 'stu_faculty': stu_faculty,
  154. 'stu_major': stu_major,
  155. }
  156. if request.method == "POST":
  157. stu_id = request.POST.get('stu_id')
  158. stu_name = request.POST.get('stu_name')
  159. stu_phone = request.POST.get('stu_phone')
  160. stu_addr = request.POST.get('stu_addr')
  161. stu_faculty = request.POST.get('stu_faculty')
  162. stu_major = request.POST.get('stu_major')
  163. # StudentInformationModel.objects.filter(stu_id=id).update(stu_id=stu_id, stu_name=stu_name, stu_phone=stu_phone, str_addr=stu_addr, stu_faculty=stu_faculty, stu_major=stu_major)
  164. # 或者 以下这种,对单个数据进行修改
  165. stu_data = StudentInformationModel.objects.get(stu_id=id)
  166. stu_data.stu_id = stu_id
  167. stu_data.stu_name = stu_name
  168. stu_data.stu_phone = stu_phone
  169. stu_data.stu_addr = stu_addr
  170. stu_data.stu_faculty = stu_faculty
  171. stu_data.stu_major = stu_major
  172. stu_data.save()
  173. context = {
  174. 'stu_id': stu_id,
  175. 'stu_name': stu_name,
  176. 'stu_phone': stu_phone,
  177. 'stu_addr': stu_addr,
  178. 'stu_faculty': stu_faculty,
  179. 'stu_major': stu_major,
  180. 'msg': '修改成功'
  181. }
  182. return render(request, 'studentManage/update.html', context)
  183. else:
  184. return render(request, 'studentManage/update.html', context)

四、功能展示

首页:

登录后

增加: 只有root用户才能增加,我默认了id为1的是root用户

修改:将当前学生信息展现出来,方便我们修改,前端页面中加了placeholder属性

删除:

             

查询

五、源码地址

https://github.com/Gscsd8527/StudentSystem

长按识别上方二维码加我个人微信,

备注666免费领取电子书

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/154046
推荐阅读
相关标签
  

闽ICP备14008679号