Django实训-学生管理系统
文章目录
- 项目总述
- 1,功能分析
- 增加学生记录,查询学生记录,修改学生记录,删除学生记录
- 2,需求分析
- 可以查询个人学生信息,包括根据学号查询学习基本情况和选课信息
- 可以更省时间的查询信息
- 一、创建Django项目-StudentSystem
- 二,创建并注册应用-studentManagement
- 1.创建studentManagement应用
- 2.注册studentManagement应用
- 三,创建数据库-studentmanager
- 四,配置MYSQL数据库连接信息
- 五,配置MYSQL数据库连接模块
- 六,创建模型
- 1.创建课程表模型-Course
- 2.创建学生信息模型-StudentInformation
- 3.创建学生用户模型-Student
- 七,数据迁移,生成课程表,学生信息表和学生用户表
- 八,给课程表,学生信息表和学生用户表添加记录
- 1.给课程表添加记录
- 2.给学生信息表添加记录
- 3.给学生用户表添加记录
- 九,创建模板页面
- 1.创建登录页面-login.html
- 2.创建主页面-index.html
- 3.创建注册页面-regist.html
- 4.创建添加记录页面-add.html
- 5.创建删除记录页面-delete.html
- 6.创建查询记录页面-select.html
- 7.创建修改记录页面-update.html
- 十,设置路由
- 1.创建子路由
- 2.创建主路由
- 十一,创建视图函数
- 1.创建主页面的视图函数-index
- 2.创建登录页面的视图函数-login
- 3.创建注册页面的视图函数-regist
- 4.创建添加页面的视图函数-add
- 5.创建查询页面的视图函数-select
- 6.创建删除页面的视图函数-delete
- 7.创建修改修改页面的视图函数-update
- 十二,启动项目,测试效果
- 1,登陆页面
- 2,注册页面
- 3,进入学生管理系统
- (1),执行增加学生信息的功能
- (2),执行查询学生信息功能
- (3),执行删除学生信息功能
- (4),执行修改学生信息功能
- 最后
项目总述
1,功能分析
增加学生记录,查询学生记录,修改学生记录,删除学生记录
2,需求分析
可以查询个人学生信息,包括根据学号查询学习基本情况和选课信息
可以更省时间的查询信息
一、创建Django项目-StudentSystem
由于我是之前就已经创建好了项目,所以此处我只是示范一番如何创建项目
二,创建并注册应用-studentManagement
1.创建studentManagement应用
-在项目配置文件里,导入os模块
- 在控制台执行
python manage.py startapp studentManagement
红色错误的原因是:我之前已经创建了该应用 - 启动项目,访问
http://127.0.0.1:8000
2.注册studentManagement应用
- 在配置文件的INSTALLED_APPS列表里添加index应用
三,创建数据库-studentmanager
四,配置MYSQL数据库连接信息
- 在settings.py文件里配置MySQL数据库连接信息
五,配置MYSQL数据库连接模块
- 在books的__init__.py文件里设置数据库连接模块
注意:如果报错,请在控制台执行pip install pymysql
安装pymysql模块
六,创建模型
1.创建课程表模型-Course
2.创建学生信息模型-StudentInformation
3.创建学生用户模型-Student
- 完整代码
from django.db import models
# 课程表
class CourseModel(models.Model):cour_id = models.CharField(max_length=15, verbose_name='学生ID')course = models.CharField(max_length=30, verbose_name='课程')grade = models.IntegerField(default=60, verbose_name='分数')class Meta():db_table = 'course'def __str__(self):return '学生Id: 课程: 分数: '.format(self.cour_id, self.course, self.grade)# 学生信息表
class StudentInformationModel(models.Model):stu_id = models.CharField(max_length=15,verbose_name='学生id')stu_name = models.CharField(max_length=30, verbose_name='学生姓名')stu_phone = models.CharField(max_length=20, verbose_name='学生电话')str_addr = models.TextField(verbose_name='学生地址')stu_faculty = models.CharField(max_length=20, verbose_name='院系')stu_major = models.CharField(max_length=30, verbose_name='专业')# 取消外键(外键是可用的)# stu_course = models.ForeignKey('CourseModel', on_delete=True)class Meta():db_table = 'studentinformation'def __str__(self):return self.stu_id# 学生用户名密码表
class StudentModel(models.Model):stu_id = models.AutoField(primary_key=True)username = models.CharField(max_length=10, verbose_name='用户名')password = models.CharField(max_length=10, verbose_name='密码')class Meta():db_table = 'student'def __str__(self):return self.username
七,数据迁移,生成课程表,学生信息表和学生用户表
- 依次执行以下命令
python manage.py makemigrations
python manage.py migrate
- 查看生成表的情况
八,给课程表,学生信息表和学生用户表添加记录
1.给课程表添加记录
insert into `course` values(1,'1001','计算机基础','90');
insert into `course` values(2,'1002','大学英语IA','99');
insert into `course` values(3,'1003','大学英语IB','99');
insert into `course` values(4,'1004','中国近代史纲要','89');
insert into `course` values(5,'1005','高等数学','85');
insert into `course` values(6,'1006','体育','95');
insert into `course` values(7,'1007','大学英语2A','99');
insert into `course` values(8,'1008','大学英语2B','100');
insert into `course` values(9,'1009','大学语文','97');
insert into `course` values(10,'1010','大学物理','96');
insert into `course` values(11,'1011','程序设计基础','98');
insert into `course` values(12,'1012','python程序设计基础','80');
insert into `course` values(13,'1013','计算机网络','87');
insert into `course` values(14,'1014','企业管理','85');
insert into `course` values(15,'1015','django开发','90');
insert into `course` values(16,'1016','形式与政策','88');
insert into `course` values(17,'1017','现代市场营销','95');
insert into `course` values(18,'1018','艺术概论','90');
insert into `course` values(19,'1019','无机化学','89');
insert into `course` values(20,'1020','工程实训','90');
- 查看课程表
2.给学生信息表添加记录
insert into `studentinformation` values (1,1,'迪丽大热巴','12345678911','四川宜宾','人工智能与大数据学院','软件技术');
insert into `studentinformation` values (2,2,'wt','12345678912','四川宜宾','人工智能与大数据学院','软件技术');
insert into `studentinformation` values (3,3,'杨潞潞','12345678913','四川广安','人工智能与大数据学院','软件技术');
insert into `studentinformation` values (4,4,'李琴琴','12345678914','四川峨眉','人工智能与大数据学院','大数据技术');
insert into `studentinformation` values (5,5,'周慧慧','12345678915','四川乐山','人工智能与大数据学院','电子信息工程技术');
insert into `studentinformation` values (6,6,'曹霖霖','12345678916','四川南充','人工智能与大数据学院','计算机应用技术');
insert into `studentinformation` values (7,7,'刘丽丽','12345678917','山东济宁','文旅学院','旅游管理');
insert into `studentinformation` values (8,8,'熊晶晶','12345678918','山东青岛','数字经济学院','电子商务');
insert into `studentinformation` values (9,9,'何晓晓','12345678919','陕西西安','数字经济学院','市场营销');
insert into `studentinformation` values (10,10,'吴磊','12345678920','河南郑州','机械工程学院','汽车制造与试验技术');
insert into `studentinformation` values (11,11,'郑小红','12345678921','河北石家庄','机械工程学院','汽车电子技术');
insert into `studentinformation` values (12,12,'冯晓宇','12345678922','贵州铜仁','智能建造学院','工程造价');
insert into `studentinformation` values (13,13,'张晓晓','12345678923','海南海口','师范学院','英语教育');
insert into `studentinformation` values (14,14,'沈莉莉','12345678924','河南沈阳','师范学院','语文教育');
insert into `studentinformation` values (15,15,'李雯雯','12345678925','湖南长沙','师范学院','学前教育');
- 查看学生信息表
3.给学生用户表添加记录
insert into `student`values (1,'迪丽大热巴','20010615');
insert into `student`values (2,'wt','19880818');
insert into `student`values (3,'杨潞潞','12345678');
insert into `student`values (4,'李琴琴','87654321');
- 查看学生用户表
九,创建模板页面
1.创建登录页面-login.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>登录页面</title>
</head>
<style>.login{text-align: center;}</style>
<body>
你好: {{ status }}
<form method="post" action="/student/login/">{% csrf_token %}<input style="text-align: center" type="text" name="username" placeholder="用户名:"/><br><input style="text-align: center" type="password" name="password" placeholder="密 码:"/><br><input class="login" type="submit" value="登录"/>
</form>
</body>
</html>
2.创建主页面-index.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>页面</title>
</head>
<style>h3{text-align: center;color: green;}.login{text-align: center;}.registered{text-align: center;}
</style>
<body>{% if lenght %}<h1 style="color: blue">欢迎{{ status}},来到学生信息管理系统</h1><br><h3 >请选择你要执行的功能</h3><a href="{% url 'student:add' %}">增加学生信息</a><br><a href="{% url 'student:select' %}">查询学生信息</a><br><a href="{% url 'student:delete' %}">删除学生信息</a><br><a href="{% url 'student:update' %}">修改学生信息</a><br><br><a href="/student/logout/">退出</a><br>
{% else %}<h3 style="color: green">请选择操作</h3><br><a href="student/login/" class="login">登录</a><br><a href="student/regist/" class="registered">注册</a><br>
{% endif %}
</body>
</html>
3.创建注册页面-regist.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>注册</title>
</head>
<body><form method="post" action="/student/regist/">{%csrf_token %}<input type="text" name="username" placeholder="用户名:"/><br><input type="password" name="password" placeholder="密 码:"/><br><input type="password" name="verif_password" placeholder="确认密码"><br>{% if error %}<h1 style="color: red">{{ error }}</h1>{% endif %}<input class="tj" type="submit" value="注册"/></form>
</body>
</html>
4.创建添加记录页面-add.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>请在下列信息栏中填入学生的基本信息</title>
</head>
<body>
<h1>请在下列信息栏中填入学生的基本信息</h1>
<form method="post" action="{% url 'student:add' %}">{% csrf_token %}学生学号:<input type="text" name="stu_id"/><br>学生姓名:<input type="text" name="stu_name"/><br>{% if msg %}<h5>{{ msg }}</h5><br>{% endif %}学生电话:<input type="text" name="stu_phone"/><br>学生地址:<input type="text" name="str_addr"/><br>学生院系:<input type="text" name="stu_faculty"/><br>学生专业:<input type="text" name="stu_major"/><br><input class="bc" type="submit" value="保存"/>{% if error %}<h5 style="color: red">{{ error }}</h5><br>{% endif %}{% if sucess %}<h5 style="color: greenyellow">{{ sucess }}</h5><br><a href=""></a>{% endif %}
</form>
</body>
</html>
5.创建删除记录页面-delete.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>请输入你要想要删除的学生学号</title>
</head>
<body>
<h1>请输入你要想要删除的学生学号</h1>
<form method="post" action="/student/delete/">{% csrf_token %}学生学号:<input type="text" name="stu_id" placeholder={{ id }}><br><input class="bc" type="submit" value="删除">
</form>
{% if msg %}{{ msg }}
{% endif %}
</body>
</html>
6.创建查询记录页面-select.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>根据学号来查询学习基本信息及选课情况</title>
</head>
<body>
<h1>根据学号来查询学习基本信息及选课情况</h1>
{% if not msg %}
{# 判断是否由msg这个参数,或者判断其值,没有的话则执行这段代码#}<form method="post" action="/student/select/">{% csrf_token %}学生学号:<input type="text" name="stu_id" placeholder={{ id }}><br><input class="bc" type="submit" value="查询"></form>
{% else %}<ul><li>学号: {{ stu_id }}</li><li>姓名: {{ stu_name }}</li><li>电话: {{ stu_phone }}</li><li>地址: {{ str_addr }}</li><li>院系: {{ stu_faculty }}</li><li>专业: {{ stu_major }}</li></ul><hr><ul>{% for k, v in course_data.items %}<li>{{ k }}: {{ v }}</li>{% endfor %}</ul>
{% endif %}
{% if error %}<h2 style="color: red">{{ error}}</h2>
{% endif %}
</body>
</html>
7.创建修改记录页面-update.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>根据学号来修改学生基本信息</title>
</head>
<body>
<h1>根据学号来修改学生基本信息</h1>
<form method="post" action="/student/update/">{% csrf_token %}学生学号:<input type="text" name="stu_id" placeholder={{ stu_id }}><br>学生姓名:<input type="text" name="stu_name" placeholder={{ stu_name }}><br>学生电话:<input type="text" name="stu_phone" placeholder={{ stu_phone }}><br>学生地址:<input type="text" name="stu_addr" placeholder={{ stu_addr }}><br>学生院系:<input type="text" name="stu_faculty" placeholder={{ stu_faculty }}><br>学生专业:<input type="text" name="stu_major" placeholder={{ stu_major }}><br><input class="bc" type="submit" value="保存">
</form>{% if msg %}{{ msg }}{% endif %}
</body>
</html>
十,设置路由
1.创建子路由
- 在index目录里创建子路由urls.py,在里面创建子路由集合urlpatterns
2.创建主路由
- 在主路由集合里添加一个路由:
path('', include('studentManagement.urls', namespace='student')),
十一,创建视图函数
** 在studentManagement的view.py里创建以下视图函数**
1.创建主页面的视图函数-index
2.创建登录页面的视图函数-login
3.创建注册页面的视图函数-regist
4.创建添加页面的视图函数-add
5.创建查询页面的视图函数-select
6.创建删除页面的视图函数-delete
7.创建修改修改页面的视图函数-update
- 综上完整代码:
from django.shortcuts import render, HttpResponse, redirect, reverse
from .models import StudentModel, StudentInformationModel, CourseModel
from django.forms.models import model_to_dict
# Create your views here.
import json
# 主界面
def index(request):context = {'status': '未登录状态'}return render(request, 'studentManage/index.html', context)# 登录界面
def login(request):if request.method == "POST":username = request.POST.get('username')password = request.POST.get('password')if not all([username, password]):return HttpResponse('错误!用户名或密码为空!')else:student = StudentModel.objects.filter(username=username, password=password)if len(student):# 将用户的信息存放到session中,session在中间件中是默认启用的request.session['user'] = {'id':student[0].stu_id,'username': username,'password': password}context = {'status': username,'msg': '已登录','lenght': 1}return render(request, 'studentManage/index.html',context)else:context = {'msg': '用户名密码错误'}return render(request, 'studentManage/login.html', context)else:context = {'status': '未登录状态','length': 0}return render(request, 'studentManage/login.html', context)
#注册界面
def regist(request):if request.method == "POST":username=request.POST.get("username")password=request.POST.get("password")verif_password=request.POST.get("verif_password")student = StudentModel.objects.filter(username=username)#注册验证错误信息汇总error_message=""if not all([username,password,verif_password]):error_message+="注册信息不能为空;\n"if student:error_message+="该用户名已存在;\n"if password!=verif_password:error_message+="两次密码输入不一致;\n"#如果存在注册信息则重定向到注册页面if error_message:context = {"error": error_message}return render(request,'studentManage/regist.html',context)#注册信息有效,注册成功stu_data = StudentModel()stu_data.username= usernamestu_data.password=passwordstu_data.save()context = {'sucess': '增加成功',}return render(request, 'studentManage/login.html', context)else:return render(request, 'studentManage/regist.html')
# 退出界面
def logout(request):# 注销掉用户,从删除session中保存的信息del request.session['user']return render(request, 'studentManage/index.html')# 增加数据 增加只能root用户或者管理员才能操作
def add(request):if request.method == "POST":root_information = request.session['user']id = root_information['id']root_id = StudentModel.objects.get(pk=id).stu_idif id == root_id:stu_name = request.POST.get('stu_name')if not stu_name:context = {'msg': '名字有遗漏',}return render(request, 'studentManage/add.html', context)stu_data = StudentInformationModel()stu_data.stu_id=idstu_data.stu_name = stu_namestu_data.stu_phone = request.POST.get('stu_phone')stu_data.str_addr = request.POST.get('str_addr')stu_data.stu_faculty =request.POST.get('stu_faculty')stu_data.stu_major = request.POST.get('stu_major')stu_data.save()context = {'sucess': '增加成功',}return render(request, 'studentManage/add.html', context)else:context = {'error': '只用root用户和管理员才能操作'}return render(request, 'studentManage/add.html', context)else:return render(request, 'studentManage/add.html')# 查询
def select(request):if request.method == "POST":id = request.POST.get('stu_id')if id=='':id=request.session['user']['id']try:stu_data = StudentInformationModel.objects.get(pk=id)except:context = {'error': "not found studnet id: "+str(id),}return render(request, 'studentManage/select.html', context)stu_course = CourseModel.objects.filter(cour_id=id)dct = {}for stu in stu_course:dct[stu.course] = stu.gradecontext = {'stu_id': id,'stu_name': stu_data.stu_name,'stu_phone':stu_data.stu_phone,'str_addr': stu_data.str_addr,'stu_faculty': stu_data.stu_faculty,'stu_major': stu_data.stu_major,'course_data': dct,'msg': True}return render(request, 'studentManage/select.html', context)# 删除else:root_information = request.session['user']id = root_information['id']context = {'msg': False,'id': id}return render(request, 'studentManage/select.html', context)# 删除
def delete(request):if request.method == "POST":id = int(request.POST.get('stu_id'))try:StudentInformationModel.objects.filter(stu_id=id).delete()except:passcontext = {'msg': '成功删除'}return render(request, 'studentManage/delete.html', context)else:root_information = request.session['user']id = root_information['id']context = {'id': id}return render(request, 'studentManage/delete.html', context)# 修改
def update(request):user_information = request.session['user']id = user_information['id']stu_data = StudentInformationModel.objects.get(stu_id=id)context = {'stu_id': stu_data.stu_id,'stu_name': stu_data.stu_name,'stu_phone':stu_data.stu_phone,'str_addr': stu_data.str_addr,'stu_faculty': stu_data.stu_faculty,'stu_major': stu_data.stu_major,}if request.method == "POST":context['stu_id'] = request.POST.get('stu_id')context['stu_name'] = request.POST.get('stu_name')context['stu_phone'] = request.POST.get('stu_phone')context['stu_addr'] = request.POST.get('stu_addr')context['stu_faculty'] = request.POST.get('stu_faculty')context['stu_major'] = request.POST.get('stu_major')return render(request, 'studentManage/update.html', context)else:return render(request, 'studentManage/update.html', context)
十二,启动项目,测试效果
1,登陆页面
- 运行此网址:
http://127.0.0.1:8000/
- 查看结果
2,注册页面
- 运行并查看
- 查看student表是否插入此数据
3,进入学生管理系统
以下功能的实现需要root用户才可以,此处默认id=1为root用户
(1),执行增加学生信息的功能
- 添加记录
- 查看studentinformation表中是否有该数据
(2),执行查询学生信息功能
输入学号为“12”的信息进行查询
查看查询结果
返回studentinformation表查看是否有该数据
(3),执行删除学生信息功能
- 输入学号为“2”的信息进行删除
- 未删除之前的studentinformation表
- 查看studentinformation表是否删除成功
(4),执行修改学生信息功能
查看未修改前的记录
修改学号为“12”的学生信息
查看数据是否被修改
最后
- 学期总结
经过了大二半学期的学习,学会了许多东西,比如,JavaScript,Django,服务器技术,数据结构的学习。还记得Django实训的时候,第一次完成项目,深刻的体会到代码整体架构的重要性,编程不能想到哪编到哪,这样在项目的后期会出现很多无法解决的致命错误,所以代码的整体架构的成败将会决定项目的成败。
Django的实训的时候,收获了许多,意识到前端的魅力,也更加坚定,我要学习它的决心,明白了Django的一些实列。在做投票项目的时候,会注入自己的的想法,化为代码,让他更有灵魂。比如,在做html页面时,会有自己的想法,让他看起来,不生硬,不死板(虽然也不是做的很好)。
综上,在接下来的学习中,要更加努力的充满兴趣的学习它,充分理解一切细节。又因为理解,所以许多原本片片断断的知识都可以渐渐互相融会贯通,累积技术能量、理论和实务之间的玻璃被打破了,学习效率才能倍增。
其次态度要端正,摆正自己的心态,不要以为什么东西上网搜搜就可以了,一定要有自己的东西。只有自己付出过,当程序运行成功时的那种喜悦才会令自己有一种莫名的自豪感。态度决定一切!然后还要有敢于挑战不安于现成的程序,要敢于用多种方法实现一个目的。
希望未来的自己,迎难而上,在不断的学习中完善自己,让自己更加完美!!!
Django实训-学生管理系统相关推荐
- 实训(学生管理系统)
实训第一步 利用百度脑图构建总体框架. 第二步创建数据库 创建相关的表 第三步在表中插入数据! 第四实现步骤 在lib里添加链接MySQL数据库的jar包 在images中添加图片 第五步创建相关的类 ...
- C语言报告书学生信息管理系统,C语言实训 学生信息管理系统
C语言实训 学生信息管理系统 实 训 报 告 实训名称 C语言编程开发实训 专业班级 物联1541 姓 名 张禄泽 学 号 指导教师 黄标兵.王丽平 实训时间 2016.2.29-2016.3.11 ...
- 大一项目实训—学生成绩管理系统
大一项目实训-学生成绩管理系统 项目实训总结 由于自己大一在Java课中没有好好听讲,导致项目实训中完全是一边学习一边敲代码.但自己付出了很多努力,早上8.30起床,晚上2,3点才睡甚至通宵,虽然做的 ...
- Java实训学生信息_(java实训)学生信息管理系统.doc
您所在位置:网站首页 > 海量文档  > 计算机 > Java (java实训)学生信息管理系统.doc9页 本文档一 ...
- 中职计算机实训室管理规定,中高职智慧实训室管理系统
中高职智慧实训室管理系统 利用物联网.互联网技术,通过智能数据控制终端设备获取前端数据资源,结合实训室管控软件平台的支撑,将实训室智能门禁.实训室视频监控.实训室远程电控.实训室温湿度检测.智慧电子门 ...
- 2021-6-28 项目实训-研究生管理系统
2021-6-28 项目实训-研究生管理系统 完成了以下工作: 1.配置Springboot+uni-app环境 2.安装MySQL数据库 3.下载安装微信小程序开发工具 4.运行示例代码
- python面向对象程序设计实训学生自我总结_实训学生自我总结
实训学生自我总结 实训学生自我总结范文 个人总结, 就是把一个时间段的个人情况进行一次全面 系统的总检查.总评价.总分析.总研究,分析成绩.不足. 经验等.下面是小编准备的实训学生自我总结,欢迎阅读. ...
- Django实训:图书信息管理系统
文章目录 一.功能实现 二.实现步骤 (一)创建Django项目 - books (二)创建并注册应用 - index 1. 创建index应用 2. 注册index应用 (三)创建数据库 - boo ...
- django实训项目——图书管理系统
(一)创建Django项目 - books 设置Django项目的位置与名称 (二).创建app应用 1.在终端中执行:python manage.py startapp apps 2.向settin ...
最新文章
- 3550配置DHCP
- 将svn设置开机启动linux,ubuntu安装SVN并设置开机启动
- C++ MD5类源文件
- 广联达2018模板算量步骤_广联达GTJ2021操作教程:造价小白3天速成,计量效率翻倍...
- McAfee:较之中国美国黑客才最令人害怕
- 软件测试:第六次作业
- 6_机器翻译与Seq2Seq模型
- 国际区块链专利统计:蚂蚁、平安、腾讯分列申请数前三名
- 蓝桥杯 ADV-222 7-2求arccos值
- 0.91寸 SSD1306 OLED介绍(四) --- 用上位机验证OLED显示屏
- 华夏银行笔试c语言,华夏银行笔试经验分享
- linux mtd 命令,Linux MTD介绍
- Android 号牌号码识别,支持新能源车牌,离线识别
- 卫星定位原理以及室内定位技术
- SystemUI修改
- (三十)AO和GO剖析
- Java_取模/取余
- excel怎么设置打印区域_如何设置打印预览,Excel打印功能介绍
- 有赞微商城打通对接知乎教程
- 协同开发冲突怎么解决?