什麼是冒泡排序 冒泡排序(Bubble Sort)也是一種簡單直觀的排序演算法。它重覆地走訪過要排序的數列,一次比較兩個元素,如果他們的順序錯誤就把他們交換過來。走訪數列的工作是重覆地進行直到沒有再需要交換,也就是說該數列已經排序完成。這個演算法的名字由來是因為越小的元素會經由交換慢慢"浮"到數列的頂端 ...
1、項目說明
基於python+Flask+mysql的學生信息管理系統項目實戰
項目需要安裝pycharm專業版,mysql資料庫以及項目所需的所有模塊
創建資料庫名稱db_online_notes,然後執行sql文件生成數據表和數據
項目需要安裝 flask,pymysql以及其他的一些模塊
安裝命令如下:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple 模塊名稱
如安裝flask:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
2、項目主要技術
python編程技術
flask框架技術
mysql資料庫技術
html編程
css編程
js編程
3、項目結構
static 項目靜態文件存儲文件夾(images,css,js)
templates 項目模板文件文件夾(html)
log.txt 錯誤日誌存放文件
forms.py 表單驗證對象
mysql_util.py 資料庫鏈接操作
manage.py 項目功能業務邏輯實現(項目入口)
4、項目主要功能
用戶登錄註冊
文章信息的增刪改查
5、部分源碼
from flask import Flask, render_template, request, redirect, session, jsonify, url_for from flask_sqlalchemy import SQLAlchemy from werkzeug.utils import secure_filename import os,shutil import random import json import pymysql import logging import math from operator import or_ from exts.QueryPaginate import QueryPaginate app = Flask(__name__,static_folder='') app.config['DEBUG']=True #配置session key app.config['SECRET_KEY'] = '2cf0fa7e-48d9-11e6-93fb-c03fd53413ef' #SQLAlchemy相關配置 app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:root@localhost:3306/student' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_ECHO'] = False # 創建組件對象 db = SQLAlchemy(app) #**********配置初始系統變數********** data = {} data['SYSNAME']='畢業設計 - 學生信息管理系統' data['DEVURL']='http://www.wwww.com/' data['DEVNAME']='上海交大' #**********資料庫配置********** #學生表 class TStudent(db.Model): __tablename__ = 't_student' id = db.Column(db.Integer, primary_key=True) student_no = db.Column(db.String(20)) student_name = db.Column(db.String(100)) department_no = db.Column(db.String(20)) #課程表 class TCourse(db.Model): __tablename__ = 't_course' id = db.Column(db.Integer, primary_key=True) course_no = db.Column(db.String(20)) course_name = db.Column(db.String(100)) #成績表 class TGrade(db.Model): __tablename__ = 't_grade' id = db.Column(db.Integer, primary_key=True) student_no = db.Column(db.String(20)) course_no = db.Column(db.String(20)) grade = db.Column(db.String(10)) #用戶表 class TUser(db.Model): __tablename__ = 't_user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20)) password = db.Column(db.String(20)) realname = db.Column(db.String(20)) #學院表 class TDepartment(db.Model): __tablename__ = 't_department' id = db.Column(db.Integer, primary_key=True) department_no = db.Column(db.String(20)) department_name = db.Column(db.String(20)) #**********路由部分********** #首頁 @app.route('/') @app.route('/index') def index(): if(checkSignIn()==False): return redirect('/signin') studentCount = db.session.query(TStudent).count() courseCount = db.session.query(TCourse).count() userCount = db.session.query(TUser).count() gradeCount = db.session.query(TGrade).count() data['studentCount']= studentCount data['courseCount']= courseCount data['userCount']= studentCount data['gradeCount']= gradeCount studentData = db.session.query(TStudent,TDepartment).outerjoin(TDepartment, TStudent.department_no==TDepartment.department_no).order_by(TStudent.id.desc()).limit(10).all() print(studentData) data['studentData']=studentData userData = db.session.query(TUser).order_by(TUser.id.desc()).limit(10).all() data['userData']=userData return render_template('index.html', **data) ''' #首頁 @app.route('/index') def index2(): if(checkSignIn()==False): return redirect('/signin') return render_template('index.html', **data) ''' #用戶登錄模塊 @app.route('/signin',methods=['GET','POST']) def signin(): return render_template('sign-in.html',form=data) #用戶退出模塊 @app.route('/signout',methods=['GET','POST']) def signout(): session.clear() #return redirect('/signin') return redirect(url_for('signin')) #用戶登錄驗證 @app.route('/dosignin',methods=['GET','POST']) def dosignin(): userName = request.values.get('userName') userPass = request.values.get('userPass') logging.info(request.remote_addr)#記錄日誌 print(request.headers)#頭文件 print(request.remote_addr)#遠程IP地址 user = db.session.query(TUser).filter(TUser.username == userName, TUser.password == userPass ).first() if(user is not None): session['userName']=userName print(session) return redirect('/') else: data['errorMessage']='用戶名或密碼不正確!' return render_template('signin.html',form=data) #用戶註冊模塊 @app.route('/signup',methods=['GET','POST']) def signup(): return render_template('sign-up.html',form=data) #學生信息部分開始************************************ #查看列錶帶分頁 @app.route('/students/',methods=['GET','POST']) def students(): studentNo = request.form.get('studentNo') if request.form.get('studentNo') is not None else '' studentName = request.form.get('studentName') if request.form.get('studentName') is not None else '' data['studentNo']=studentNo data['studentName']=studentName page = int(request.form.get('search_page')) if request.form.get('search_page') is not None and request.form.get('search_page') != '0' else 1 query = db.session.query(TStudent).filter(or_(TStudent.student_no == studentNo, studentNo == ''), or_(TStudent.student_name.like('%{0}%'.format(studentName)),studentName == '') ) queryPaginate = QueryPaginate(query,10,page) data['currdata']=queryPaginate.items data['pagelist']=queryPaginate.iter_pages() data['pages']=queryPaginate.pages data['page']=queryPaginate.page data['per_page']=queryPaginate.per_page data['prev_num']=queryPaginate.prev_num data['next_num']=queryPaginate.next_num return render_template('students.html',**data) #查看單個信息 @app.route('/student/<int:studentid>',methods=['GET','POST']) def student(studentid): data['studentid']=studentid currdata = db.session.query(TStudent).filter(TStudent.id==studentid).first() data['currdata'] = currdata return render_template('student.html',**data) #編輯單個信息 @app.route('/student/edit/<int:studentid>',methods=['GET']) def student_edit(studentid): currdata = db.session.query(TStudent).filter(TStudent.id==studentid).first() print(currdata) data['currdata'] = currdata return render_template('student_edit.html',**data) #編輯保存單個信息 @app.route('/student/doedit/',methods=['POST']) def student_doedit(): studentid = request.form.get('studentid') studentNo = request.form.get('studentNo') studentName = request.form.get('studentName') update_dict = {} if(studentNo is not None): update_dict['student_no']=studentNo if(studentName is not None): update_dict['student_name']=studentName db.session.query(TStudent).filter_by(id=studentid).update(update_dict) db.session.commit() print(data) return redirect(url_for('students')) #刪除單個信息 @app.route('/student/del/<int:studentid>',methods=['GET']) def student_del(studentid): db.session.query(TStudent).filter_by(id=studentid).delete() db.session.commit() return redirect(url_for('students')) #刪除單個信息 @app.route('/student/dodel/',methods=['POST']) def student_dodel(studentid): db.session.query(TStudent).filter_by(id=studentid).delete() db.session.commit() return redirect(url_for('students')) #保存單個信息 @app.route('/student/add',methods=['GET']) def student_add(): return render_template('student_add.html',form=data) #保存單個信息 @app.route('/student/doAdd',methods=['POST']) def student_doadd(): studentNo = request.form.get('studentNo') studentName = request.form.get('studentName') print(request.form) student = TStudent(student_no=studentNo,student_name=studentName) db.session.add(student) db.session.commit() return redirect(url_for('students')) #學生信息部分結束************************************ #課程信息部分開始************************************ #查看列錶帶分頁 @app.route('/courses/',methods=['GET','POST']) def courses(): courseNo = request.form.get('courseNo') if request.form.get('courseNo') is not None else '' courseName = request.form.get('courseName') if request.form.get('courseName') is not None else '' data['courseNo']=courseNo data['courseName']=courseName page = int(request.form.get('search_page')) if request.form.get('search_page') is not None and request.form.get('search_page') != '0' else 1 query = db.session.query(TCourse).filter(or_(TCourse.course_no == courseNo, courseNo == ''), or_(TCourse.course_name.like('%{0}%'.format(courseName)),courseName == '') ) queryPaginate = QueryPaginate(query,10,page) data['currdata']=queryPaginate.items data['pagelist']=queryPaginate.iter_pages() data['pages']=queryPaginate.pages data['page']=queryPaginate.page data['per_page']=queryPaginate.per_page data['prev_num']=queryPaginate.prev_num data['next_num']=queryPaginate.next_num return render_template('courses.html',**data) #查看單個信息 @app.route('/course/<int:courseid>',methods=['GET','POST']) def course(courseid): data['courseid']=courseid currdata = db.session.query(TCourse).filter(TCourse.id==courseid).first() data['currdata'] = currdata return render_template('course.html',**data) #編輯單個信息 @app.route('/course/edit/<int:courseid>',methods=['GET']) def course_edit(courseid): currdata = db.session.query(TCourse).filter(TCourse.id==courseid).first() print(currdata) data['currdata'] = currdata return render_template('course_edit.html',**data) #編輯保存單個信息 @app.route('/course/doedit/',methods=['POST']) def course_doedit(): courseid = request.form.get('courseid') courseNo = request.form.get('courseNo') courseName = request.form.get('courseName') update_dict = {} if(courseNo is not None): update_dict['course_no']=courseNo if(courseName is not None): update_dict['course_name']=courseName db.session.query(TCourse).filter_by(id=courseid).update(update_dict) db.session.commit() print(data) return redirect(url_for('courses')) #刪除單個信息 @app.route('/course/del/<int:courseid>',methods=['GET']) def course_del(courseid): db.session.query(TCourse).filter_by(id=courseid).delete() db.session.commit() return redirect(url_for('courses')) #刪除單個信息 @app.route('/course/dodel/',methods=['POST']) def course_dodel(courseid): db.session.query(TCourse).filter_by(id=courseid).delete() db.session.commit() return redirect(url_for('courses')) #保存單個信息 @app.route('/course/add',methods=['GET']) def course_add(): return render_template('course_add.html',form=data) #保存單個信息 @app.route('/course/doAdd',methods=['POST']) def course_doadd(): courseNo = request.form.get('courseNo') courseName = request.form.get('courseName') print(request.form) course = TCourse(course_no=courseNo,course_name=courseName) db.session.add(course) db.session.commit() return redirect(url_for('courses')) #課程信息部分結束************************************ #成績信息部分開始************************************ @app.route('/grades/',methods=['GET','POST']) def grades(): studentNo = request.form.get('studentNo') if request.form.get('studentNo') is not None else '' studentName = request.form.get('studentName') if request.form.get('studentName') is not None else '' courseNo = request.form.get('courseNo') if request.form.get('courseNo') is not None else '' courseName = request.form.get('courseName') if request.form.get('courseName') is not None else '' data['studentNo']=studentNo data['studentName']=studentName data['courseNo']=courseNo data['courseName']=courseName page = int(request.form.get('search_page')) if request.form.get('search_page') is not None and request.form.get('search_page') != '0' else 1 query = db.session.query(TGrade,TStudent,TCourse).outerjoin(TStudent, TStudent.student_no==TGrade.student_no).outerjoin(TCourse,TCourse.course_no==TGrade.course_no).filter(or_(TCourse.course_no == courseNo, courseNo == ''), or_(TCourse.course_name.like('%{0}%'.format(courseName)),courseName == ''), or_(TStudent.student_no == studentNo, studentNo == ''), or_(TStudent.student_name.like('%{0}%'.format(studentName)),studentName == '') ) #print(query) queryPaginate = QueryPaginate(query,10,page) #print(queryPaginate.items) data['currdata']=queryPaginate.items data['pagelist']=queryPaginate.iter_pages() data['pages']=queryPaginate.pages data['page']=queryPaginate.page data['per_page']=queryPaginate.per_page data['prev_num']=queryPaginate.prev_num data['next_num']=queryPaginate.next_num return render_template('grades.html',**data) #保存單個信息 @app.route('/grade/add',methods=['GET']) def grade_add(): studentdata = db.session.query(TStudent).all() data['studentdata']=studentdata coursedata = db.session.query(TCourse).all() data['coursedata']=coursedata return render_template('grade_add.html',**data) #保存單個信息 @app.route('/grade/doAdd',methods=['POST']) def grade_doadd(): courseNo = request.form.get('courseNo') studentNo = request.form.get('studentNo') grade = request.form.get('grade') grade = TGrade(course_no=courseNo,student_no=studentNo,grade=grade) db.session.add(grade) db.session.commit() return redirect(url_for('grades')) #刪除單個信息 @app.route('/grade/del/<int:gradeid>',methods=['GET']) def grade_del(gradeid): db.session.query(TGrade).filter_by(id=gradeid).delete() db.session.commit() return redirect(url_for('grades')) #成績信息部分結束************************************ #用戶信息部分開始************************************ #查看列表 @app.route('/users',methods=['GET','POST']) def users(): userName = request.form.get('userName') if request.form.get('userName') is not None else '' realName = request.form.get('realName') if request.form.get('realName') is not None else '' data['userName']=userName data['realName']=realName page = int(request.form.get('search_page')) if request.form.get('search_page') is not None and request.form.get('search_page') != '0' else 1 query = db.session.query(TUser).filter(or_(TUser.username == userName, userName == ''), or_(TUser.realname.like('%{0}%'.format(realName)),realName == '') ) print(query) queryPaginate = QueryPaginate(query,10,page) #print(queryPaginate.items) data['currdata']=queryPaginate.items data['pagelist']=queryPaginate.iter_pages() data['pages']=queryPaginate.pages data['page']=queryPaginate.page data['per_page']=queryPaginate.per_page data['prev_num']=queryPaginate.prev_num data['next_num']=queryPaginate.next_num return render_template('users.html',**data) #查看單個信息 @app.route('/user/<int:userid>',methods=['GET','POST']) def user(userid): data['userid']=userid print(data) return render_template('user.html',form=data) #保存單個信息 @app.route('/user/add',methods=['GET']) def user_add(): return render_template('user_add.html',**data) #保存單個信息 @app.route('/user/doAdd',methods=['POST']) def user_doadd(): username = request.form.get('username') password = request.form.get('password') realname = request.form.get('realname') user = TUser(username=username,password=password,realname=realname) db.session.add(user) db.session.commit() return redirect(url_for('users')) #編輯單個信息 @app.route('/user/edit/<int:userid>',methods=['GET']) def user_edit(userid): currdata = db.session.query(TUser).filter(TUser.id==userid).first() print(currdata) data['currdata'] = currdata return render_template('user_edit.html',**data) #編輯保存單個信息 @app.route('/user/doedit/',methods=['POST']) def user_doedit(): userid = request.form.get('userid') username = request.form.get('username') password = request.form.get('password') realname = request.form.get('realname') update_dict = {} if(username is not None): update_dict['username']=username if(password is not None): update_dict['password']=password if(realname is not None): update_dict['realname']=realname db.session.query(TUser).filter_by(id=userid).update(update_dict) db.session.commit() print(data) return redirect(url_for('users')) #刪除單個信息 @app.route('/user/del/<int:userid>',methods=['GET']) def user_del(userid): db.session.query(TUser).filter_by(id=userid).delete() db.session.commit() return redirect(url_for('users')) #用戶信息部分結束************************************ #用戶設置部分結束************************************ #編輯單個信息 @app.route('/user/set/<username>',methods=['GET']) def user_set(username): currdata = db.session.query(TUser).filter(TUser.username==username).first() print(currdata) data['currdata'] = currdata return render_template('user_set.html',**data) #編輯保存單個信息 @app.route('/user/doset/',methods=['POST']) def user_doset(): userid = request.form.get('userid') username = request.form.get('username') password = request.form.get('password') realname = request.form.get('realname') update_dict = {} if(username is not None): update_dict['username']=username if(password is not None): update_dict['password']=password if(realname is not None): update_dict['realname']=realname db.session.query(TUser).filter_by(id=userid).update(update_dict) db.session.commit() print(data) return redirect(url_for('users')) #用戶設置部分結束************************************ @app.route('/faq',methods=['GET','POST']) def faq(): return render_template('faq.html',**data) @app.route('/help',methods=['GET','POST']) def help(): return render_template('help.html',**data) @app.route('/calendar',methods=['GET','POST']) def calendar(): return render_template('calendar.html',form=data) @app.route('/media',methods=['GET','POST']) def media(): return render_template('media.html',**data) @app.route('/theory',methods=['GET','POST']) def theory(): return render_template('theory.html',**data) @app.route('/privacy-policy',methods=['GET','POST']) def privacypolicy(): return render_template('privacy-policy.html',**data) @app.route('/terms-and-conditions',methods=['GET','POST']) def termsandconditions(): return render_template('terms-and-conditions.html',**data) #系統級報警路由 @app.errorhandler(403) def error403(): return render_template('403.html',form=data) @app.errorhandler(404) def error404(error): return render_template('404.html',form=data) @app.errorhandler(500) def error500(): return render_template('500.html',form=data) @app.errorhandler(503) def error503(): return render_template('503.html',form=data) #**********測試用路由********** @app.route('/jsontest') def jsonTest(): jsonStr = '{"name":"張三","age":"33"}' jsonData = json.loads(jsonStr) print(jsonData['name']) data={"name":"張三","age":"33"} print (json.dumps(data,ensure_ascii=False)) #return jsonify(data) return jsonify(name="張三",age=34) @app.route('/mysqltest') def mysqlTest(): cursor = db.cursor() try: mySql = "select * from kelly limit 10" cursor.execute(mySql) data = cursor.fetchall() except Exception as e: print("Error: ",e) return jsonify(data) #**********方法部分********** #檢測登錄狀態 def checkSignIn(): #if(session.get('userName') is None or session.get('userName')==''): if('userName' not in session): return False else: return True #**********通用方法部分********** if __name__ == "__main__": app.run(host='0.0.0.0',debug=False,port=8088) #app.run(host='127.0.0.1',debug=False,port=8082)
6、運行截圖
7、項目總結
本項目是一個非常適合練手的項目,對我們的python編程和對flask框架的瞭解和提升都有很大的幫助,推薦大家學習研究這個項目,搞懂其中的原理流程以及知識點非常關鍵。
資料獲取地址:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.4fee3d0dmeAXHu&ft=t&id=713204492149
B站視頻講解地址:
註:其他問題請參看視頻講解,都有介紹,一定要認真看完哦!
代碼編寫、視頻錄製不易,感謝您的支持,祝您學習愉快!