主页 > 编程 > python >

python 和mysql 编写学生管理系统用户登录模块

2019-01-13 18:25 阅读:67 来源:智宇SEO自媒体

  运行环境:win10 64位 python3.5   Flask 0.12  pymysql 0.7.11   mysql 5.5

  文件:login.py(登录页面)  db.py(登录验证数据库操作)

  功能:完成学生用户登录验证

python 和mysql 编写学生管理系统用户登录模块

  login.py文件代码:

from flask import Flask,render_template,request,url_for,redirect
from datetime import timedelta
from db import *
app = Flask(__name__)
#静态文件缓存时间设置
app.config['SEND_FILE_MAX_AGE_DEFAULT']=timedelta(seconds=1)
#登录
@app.route('/index',methods=['GET','POST'])
def index():
    return "恭喜,登录成功"
@app.route('/',methods=['GET','POST'])
def login():
    if request.method == 'POST':
        name = request.form['name']
        pwd = request.form['pwd']
        if loginCheck(name,pwd):
            return redirect(url_for('index'))
        else:
            message = "用户名或者密码错误"
            return render_template('login.html',message=message)
    return render_template('login.html')

if __name__ == '__main__':
    app.run()

  db.py文件代码

import pymysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="root", db="stu", charset="utf8")
cur = conn.cursor()
sql = "select * from userstu"
def loginCheck(name,pwd):
    sql="select * from userstu where stuname='%s' and password='%s'" %(name,pwd)
    cur.execute(sql)
    result=cur.fetchall()
    if (len(result))==0:
        return False
    else:
        return True

  mysql 数据结构:

DROP TABLE IF EXISTS `userstu`;
CREATE TABLE `userstu` (
  `stuname` varchar(20) NOT NULL,
  `stuid` varchar(15) NOT NULL,
  `sex` varchar(1) NOT NULL,
  `birth` datetime NOT NULL,
  `College` varchar(20) NOT NULL,
  `photo` varchar(50) NOT NULL,
  `profile` text NOT NULL,
  `password` varchar(20) DEFAULT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;


完整代码下载:链接: https://pan.baidu.com/s/1-1kfmDS7tONAGj2JSdIAQA 提取码: 4ftu