【学员管理系统】0x02 学生信息管理功能

写在前面

项目详细需求参见Django项目之【学员管理系统】

Django框架大致处理流程

捋一下Django框架相关的内容:

浏览器输入URL到页面展示结果的过程,可以简单参考下图:

上图中,绿色部分就是我们实际需要开发的那部分。

上一篇博客实现了班级信息的增删改查,现在继续开发学生信息管理的功能。

学生信息管理相比于班级信息管理稍微难一点,因为数据库的表结构中学生表通过外键关联了班级表。

所以增删改查的操作需要注意外键部分的相关操作。

学生信息展示(查)

区别于班级信息管理,学生信息因为通过外键关联了班级信息,所以除了要展示学生的姓名还要展示出学生所属班级的名称。

后端部分

def student_list(request):
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="root1234", db="mysite", charset="utf8")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("SELECT student.id, student.name, class.name AS class_name from student LEFT JOIN class ON student.class_id = class.id;")
student_list = cursor.fetchall()
cursor.close()
conn.close()
return render(request, "student_list.html", {"students": student_list})

后端部分

前端部分

<table class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>学生姓名</th>
<th>班级</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for student in students %} <tr>
<th scope="row">{{ student.id }}</th>
<td>{{ student.name }}</td>
<td>{{ student.class_name }}</td>
<td class="text-center">
<a type="button" class="btn btn-sm btn-success" aria-label="Left Align">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>编辑
</a>
|
<a href="/edit_student/?student_id={{ student.id }}/" type="button" class="btn btn-sm btn-success" aria-label="Left Align">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>新页面编辑
</a>
|
<a href="/delete_student/?student_id={{ student.id }}" type="button" class="btn btn-sm btn-danger" aria-label="Left Align">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
</a>
</td>
</tr>
{% endfor %} </tbody>
</table>

前端部分

删除学生信息(删)

后端部分

def delete_student(request):
# 从GET请求的URL中取到要删除的学生ID
student_id = request.GET.get("student_id")
# 连接数据库
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="root1234", db="mysite", charset="utf8")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 删除指定的学生
sql = "delete from student WHERE id=%s;"
# 执行SQL语句
cursor.execute(sql, [student_id, ])
conn.commit()
conn.close()
# 删除成功,跳转到学生列表页
return redirect("/student_list/")

后端部分

前端部分

注意在学生信息的页面删除按钮上用模板语言的方式拼接student_id。

<a href="/delete_student/?student_id={{ student.id }}" type="button" class="btn btn-sm btn-danger" aria-label="Left Align">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
</a>

前端部分

添加学生信息(增)

后端部分

def add_student(request):
# 如果是POST请求表示前端提交数据过来
if request.method == "POST":
student_name = request.POST.get("student_name")
class_id = request.POST.get("class_id")
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="root1234", db="mysite", charset="utf8")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("insert into student(name, class_id) VALUES (%s, %s)", [student_name, class_id])
conn.commit()
cursor.close()
conn.close()
return redirect("/student_list/")
# 前端不发送POST请求情况下默认返回新增学生信息页面
else:
# 因为我们新添加学生信息的时候需要指定所属的班级
# 所以需要先查询出所有的班级信息,填充到页面上
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="root1234", db="mysite", charset="utf8")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select id, name from class")
class_list = cursor.fetchall()
cursor.close()
conn.close()
return render(request, "add_student.html", {"class_list": class_list})

后端部分

前端部分

前端页面需要将已经有的班级信息做成可以选择的select框。

<form class="form-horizontal" action="/add_student/" method="post">
<div class="form-group">
<label for="inputclassname" class="col-sm-2 control-label">学生姓名</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="student_name" id="inputclassname" placeholder="学生姓名">
</div>
</div>
<div class="form-group">
<label for="selectclass" class="col-sm-2 control-label">班级</label>
<div class="col-sm-10">
<select class="form-control" name="class_id">
{% for class in class_list %}
<option value="{{ class.id }}">{{ class.name }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>

前端部分

编辑学生信息(改)

后端部分

def edit_student(request):
if request.method == "POST":
student_id = request.POST.get("student_id")
student_name = request.POST.get("student_name")
class_id = request.POST.get("class_id")
# 更新学生表的SQL
sql = "update student set name=%s, class_id= %s WHERE id=%s;"
# 连接数据库
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="root1234", db="mysite", charset="utf8")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute(sql, [student_name, class_id, student_id])
cursor.close()
conn.close()
# 更新完学生信息之后跳转到学生列表页面
return redirect("/student_list/")
else:
# 要编辑学生信息就需要在页面上把当前学生的信息以及所有的班级信息都展示出来
# 取到要编辑的学生的ID
student_id = request.GET.get("student_id")
# 连接数据库
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="root1234", db="mysite", charset="utf8")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 取到所有的班级信息
get_class_sql = "select id, name from class;"
cursor.execute(get_class_sql)
class_list = cursor.fetchall()
get_student_sql = "select id, name, class_id from student where id=%s;"
cursor.execute(get_student_sql, [student_id, ])
student = cursor.fetchone()
cursor.close()
conn.close()
return render(request, "edit_student.html", {"class_list": class_list, "student": student})

后端部分

前端部分

<form class="form-horizontal" action="/edit_student/" method="post">
<input type="text" name="student_id" value="{{ student.id }}" style="display: none">
<div class="form-group">
<label for="inputclassname" class="col-sm-2 control-label">学生姓名</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="student_name" id="inputclassname" placeholder="班级名称" value="{{ student.name }}">
</div>
<span id="helpBlock2" class="help-block">{{ error }}</span>
</div>
<div class="form-group">
<label for="selectclass" class="col-sm-2 control-label">班级</label>
<div class="col-sm-10">
<select class="form-control" name="class_id">
{% for class in class_list %}
{% if class.id == student.class_id %}
<option selected value="{{ class.id }}">{{ class.name }}</option>
{% else %}
<option value="{{ class.id }}">{{ class.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>

前端部分

【学员管理系统】0x02 学生信息管理功能的更多相关文章

  1. 使用Struts2+Hibernate开发学生信息管理功能1

    第一章:Struts2与Hibernate整合 1.课程简介 2.界面原型演示 3.Struts2与Hibernate整合 4.创建实体类 5.生成实体映射文件 6.生成表结构 1.课程简介 Stru ...

  2. 学员管理系统(简单的Django设计)

    学员管理系统(简单的Django设计) 学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司 ...

  3. Django pymysql学员管理系统

    学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司急需一套方便易用的“学员管理系统”,来提 ...

  4. 【学员管理系统】0x03 老师信息管理功能

    [学员管理系统]0x03 老师信息管理功能 老师信息管理相比于学生信息管理又多了一点,因为我们的数据结构中老师表和班级表是通过teacher2class表进行多对多关联的. 写在前面 项目详细需求参见 ...

  5. 【学员管理系统】0x01 班级信息管理功能

    [学员管理系统]0x01 班级信息管理功能 写在前面 项目详细需求参见:Django项目之[学员管理系统] 视图函数: 我们把所有的处理请求相关的函数从 urls.py中拿出来,统一放在一个叫view ...

  6. 一个低级shell简易学生信息管理系统-新增登陆注册功能

    还有bug 不修改了 小声bb一下 这玩意真的要控制版本 随手保存 本来有个超完整的版本 一开心被我rm - f 了 后续还出现了 更多的bug 仔细仔细 源码如下: record=stu.db if ...

  7. Python开发程序:学员管理系统(mysql)

    主题:学员管理系统 需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图: 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节 ...

  8. Python学习(二十七)—— Django和pymysql搭建学员管理系统

    转载自http://www.cnblogs.com/liwenzhou/p/8270250.html 一.学员管理系统 1.项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的e ...

  9. Django和pymysql搭建学员管理系统

    学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司急需一套方便易用的“学员管理系统”,来提 ...

随机推荐

  1. navigationItem的设置和titleView的设置

    设置导航栏中间的标题 self.navigationItem.title = @"title"; 设置导航栏的主题颜色 self.navigationBar.barTintColo ...

  2. MySQL的四种变量类型

    一.全局变量在系统运行期间动态更改其参数,重启后失效.SET GLOABL var=XXX;SET @@global.var=XXX;以上两种方式等效 查看系统的全局变量show global var ...

  3. USB驱动 | win10下使用console线错误代码10解决办法

    驱动安装后如果无法使用,错误代码10, 原因:系统自动安装了新版本的驱动,回滚到老版本即可

  4. C语言可以给字符数组赋值的方法

    分类: C 2012-04-06 10:23 4081人阅读 评论(0) 收藏 举报 语言c 学了这么多年的C语言,突然发现连字符串赋值都出错,真的很伤心. char a[10]; 怎么给这个数组赋值 ...

  5. TCP应用程序通信协议的处理

    TCP应用程序通信协议的处理 flyfish 2015-6-29 一 流式处理 TCP是一种流协议(stream protocol).TCP数据是以字节流的形式传递给接收者的,没有固有的"报 ...

  6. mvc Ajax 跨域请求

    js端: $.ajax({ type : "get", async : false, url :url, data: 'bid=0&xingming=' + uName + ...

  7. CentOS6安装和卸载docker

    系统版本 [root@bogon yum.repos.d]# uname -a Linux bogon 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 ...

  8. sublime livereload插件

    1.首先在chrome商店下载livereload 安装之后记得在 chrome 的 扩展程序 里面 勾上 允许访问文件地址 2.sublime text 3 中下载插件 Livereload 3.配 ...

  9. Ubuntu安装sublime test 3 (Build 3126)

    Ubuntu下 Sublime Text 3 (Build 3143) 存在一些bug........ 满心欢喜地更新后, 又默默换回Build 3126 1. 安装 sudo apt-get upd ...

  10. awk.sed.grep三剑客详解

    事前准备1.主机node1:172.16.133.112.作为实验的文件/etc/passwd /etc/fstab qinqin cp /etc/passwd . cp /etc/fstab . 一 ...