【学员管理系统】0x02 学生信息管理功能
【学员管理系统】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 学生信息管理功能的更多相关文章
- 使用Struts2+Hibernate开发学生信息管理功能1
第一章:Struts2与Hibernate整合 1.课程简介 2.界面原型演示 3.Struts2与Hibernate整合 4.创建实体类 5.生成实体映射文件 6.生成表结构 1.课程简介 Stru ...
- 学员管理系统(简单的Django设计)
学员管理系统(简单的Django设计) 学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司 ...
- Django pymysql学员管理系统
学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司急需一套方便易用的“学员管理系统”,来提 ...
- 【学员管理系统】0x03 老师信息管理功能
[学员管理系统]0x03 老师信息管理功能 老师信息管理相比于学生信息管理又多了一点,因为我们的数据结构中老师表和班级表是通过teacher2class表进行多对多关联的. 写在前面 项目详细需求参见 ...
- 【学员管理系统】0x01 班级信息管理功能
[学员管理系统]0x01 班级信息管理功能 写在前面 项目详细需求参见:Django项目之[学员管理系统] 视图函数: 我们把所有的处理请求相关的函数从 urls.py中拿出来,统一放在一个叫view ...
- 一个低级shell简易学生信息管理系统-新增登陆注册功能
还有bug 不修改了 小声bb一下 这玩意真的要控制版本 随手保存 本来有个超完整的版本 一开心被我rm - f 了 后续还出现了 更多的bug 仔细仔细 源码如下: record=stu.db if ...
- Python开发程序:学员管理系统(mysql)
主题:学员管理系统 需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图: 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节 ...
- Python学习(二十七)—— Django和pymysql搭建学员管理系统
转载自http://www.cnblogs.com/liwenzhou/p/8270250.html 一.学员管理系统 1.项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的e ...
- Django和pymysql搭建学员管理系统
学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司急需一套方便易用的“学员管理系统”,来提 ...
随机推荐
- 01.Hello Node.js
程序下载:https://files.cnblogs.com/files/xiandedanteng/helloNodejs.rar 关键代码: var http=require('http'); v ...
- kali渗透综合靶机(一)--Lazysysadmin靶机
kali渗透综合靶机(一)--Lazysysadmin靶机 Lazysysadmin靶机百度云下载链接:https://pan.baidu.com/s/1pTg38wf3oWQlKNUaT-s7qQ提 ...
- 渐进式 JPEG (Progressive JPEG)来提升用户体验
1.概述 jpg格式分为:Baseline JPEG(标准型)和Progressive JPEG(渐进式).两种格式有相同尺寸以及图像数据,扩展名也是相同的,唯一的区别是二者显示的方式不同. Base ...
- Git学习小结
版本控制工具 集中式: CVS SVN 集大成者 分布式:git 创始人:inux Towards 2005年 工具 最好使用linux(oh-my-zsh) gitbash -> cygwin ...
- Install Erlang and Elixir in CentOS 7
In this tutorial, we will be discussing about how to install Erlang and Elixir in CentOS 7 minimal s ...
- Java学习从入门到精通(2) [转载]
Java Learning Path(二).书籍篇 学习一门新的知识,不可能指望只看一本,或者两本书就能够完全掌握.需要有一个循序渐进的阅读过程.我推荐Oreilly出版的Java系列书籍. 在这里我 ...
- ajax个人学习笔记
1. function createXHR(){ if(typeof XMLHttpRequest != 'undefined'){ return new XMLHttpRequest(); }els ...
- 属性字符串NSMutableAttributedString
要实现如下效果: NSString * mailString = @"mymail@126.com"; NSString * mailStringWithQuotes = [NSS ...
- json对象和json字符串之间的转换-JavaScript实现
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Mac下安装LNMP(Nginx+PHP5.6)环境(转)
安装Homebrew 最近工作环境切换到Mac,所以以OS X Yosemite(10.10.1)为例,记录一下从零开始安装Mac下LNMP环境的过程 确保系统已经安装xcode,然后使用一行命令安装 ...