以老师身份登录(账号和 密码 都是 admin)
需求分析
掌握面向对象思想,类的定义,对象的创建及类中各种方法的定义;
研究并掌握Python面向对象的语法特点和使用方法; 培养自己的业务逻辑能力,提高思维能力;
用Python对mysql数据库的操作(增删改查);
Python对字符串操作 ,所用到的函数;
前端与后端的交互; django的使用方法掌握(MTV)思想;
前端页面的开发,rem布局,bootstrap框架,js,jquery,ajax异步加载的操作熟练度有所提升;
用Echart对数据可视化操作,以图来展示学生成绩使数据更加清晰;
总结和体会
第一次用python写web项目,有的地方不熟悉。但是让我了解到django这个框架便捷和优点。 虽然django提供一套操作数据库的框架但是我用起来十分别扭,因为里面不含sql语句,然后我想着就用pymysql操作数据库。 项目中还有一个缺点控制层和业务层没有分开,主要是我的这个项目简单,业务逻辑不多,对象也很少。
在写项目的过程中我发现自己sql 语句不够熟练,很多时候报错都是因为sql语句错误。以后我会多花点时间练习sql语句。
总的来说这次项目不仅仅是为了完成作业, 主要是对我前些时间学习web前端的一个回顾,检测一下我的学习成果。让我对python有了重新的认识,不得不说python实在是太牛了!!!
功能设计与分析
后端核心代码
View.py 数据库操作层
from django.http import HttpResponse
from django.shortcuts import render
import pymysql
import json
def getConn():
global conn, cursor
# 连接数据库
conn = pymysql.connect(host='127.0.0.1', port=3306, db='student', user='root', password='root')
# 创建游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
return cursor
# 关闭数据库
def closeConn(nn):
if nn != 0:
conn.commit()
cursor.close()
conn.close()
def login(request):
return render(request, 'login.html')
# 获取前段登录传过来的值
def loginDao(request):
request.encoding = 'utf8'
# 清除session域
request.session.flush()
username = request.POST['username']
password = request.POST['password']
identify = request.POST['identify']
# 抛出异常 防止查询失败程序报错
try:
# 获取数据库连接
cs = getConn()
# 执行sql查询
if identify == 'student':
cs.execute("select * from StudentModel_student where sno={0} and password={1};".format(username, password))
student = cs.fetchone()
request.session['user'] = student['name']
closeConn(0)
return render(request, "studentPanel.html", {"student": student})
else:
cs.execute(
"select * from StudentModel_teacher where cno='{0}' and password='{1}';".format(username, password))
request.session['user'] = cs.fetchone()['cno']
closeConn(0)
students = AllStudents()
return render(request, "managerPanel.html", {'students': students})
except Exception:
closeConn(0)
return render(request, "errorMsg.html", {"error": "用户名/密码错误"})
# 查看所有学生信息
def AllStudents():
cs = getConn()
cs.execute("select * from StudentModel_student order by Totall desc;")
students = cs.fetchall()
closeConn(0)
return students
# 查寻个人
def searchStudent(sno):
cs = getConn()
cs.execute("select * from StudentModel_student where sno='{0}';".format(sno))
student = cs.fetchone()
closeConn(0)
return student
# 添加学生信息
def addStudent(request):
request.encoding = 'utf8'
name = request.POST['name']
sno = request.POST['sno']
password = request.POST['password']
math = int(request.POST['math'])
chinese = int(request.POST['chinese'])
english = int(request.POST['english'])
try:
cs = getConn()
cs.execute("select * from StudentModel_student where sno='{0}'".format(sno))
if cs.rowcount > 0:
closeConn(0)
return render(request, "errorMsg.html", {'error': "数据库中已有该学生,不能重复添加!!!"})
else:
cs.execute(
"insert into StudentModel_student values('{0}','{1}','{2}',{3},{4},{5},{6})".format(name, sno, password,
math,
chinese, english,
math + chinese + english))
closeConn(1)
students = AllStudents()
return render(request, "managerPanel.html", {'students': students})
except Exception:
closeConn(0)
return render(request, "errorMsg.html", {'error': "数据库操作失败请在再试一次!!!"})
# ajax回调函数查看个人
def findOne(request):
sno = request.POST['sno']
student = searchStudent(sno)
return HttpResponse(json.dumps(student))
# 修改信息
def modifyStudent(request):
sno = request.GET.get('sno')
math = request.POST.get('math')
print(math)
chinese = request.POST.get('chinese')
english = request.POST.get('english')
student = searchStudent(sno)
if math == None or math.strip() == '':
math = student['Math']
if english == None or english.strip() == '':
english = student['English']
if chinese == None or chinese.strip() == '':
chinese = student['Chinese']
totall = int(math) + int(english) + int(chinese)
try:
cs = getConn()
cs.execute(
"update StudentModel_student set Math = {0} , English ={1} , Chinese={2} , Totall={3} where sno = '{4}'".format(
int(math), int(english), int(chinese), totall, sno))
closeConn(1)
students = AllStudents()
return render(request, "managerPanel.html", {'students': students})
except Exception:
closeConn(0)
return render(request, "errorMsg.html", {'error': "数据更新失败请在再试一次!!!"})
# 删除学生信息
def DeleteStudent(request):
sno = request.GET['sno']
try:
cs = getConn()
cs.execute("delete from StudentModel_student where sno = '{0}'".format(sno))
closeConn(1)
students = AllStudents()
return render(request, "managerPanel.html", {'students': students})
except Exception:
closeConn(0)
return render(request, "errorMsg.html", {'error': "数据删除失败请在再试一次!!!"})
# ajax 回调所有信息
def showAllstudents(request):
cs = getConn()
cs.execute("select * from StudentModel_student;")
students = cs.fetchall()
closeConn(0)
return HttpResponse(json.dumps(students))