码迷,mamicode.com
首页 > 数据库 > 详细

[Python-Flask-SQLite]学生管理系统V1.0

时间:2020-07-13 09:39:10      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:exe   with   rollback   port   ==   connect   admin   into   not   

Python-Flask-SQLite 学生管理系统V1.0

登录/注册/管理员登录/增删改查(未完成)

技术图片技术图片技术图片技术图片

 

 

 

 

 

1.创建学生表单

import sqlite3 as sql

#连接数据库,若不存在则自动创建stu.db
conn = sql.connect("stu.db")
#创建cursor
cur = conn.cursor()
#创建students表单
create_table = ‘‘‘
    CREATE TABLE students(
    username TEXT PRIMARY KEY NOT NULL,
    number INT NOT NULL,
    college TEXT NOT NULL,
    major TEXT NOT NULL,
    password TEXT NOT NULL,
    is_admin NUMERIC DEFAULT 0 NOT NULL)
‘‘‘
#执行数据库语句
cur.execute(create_table)
print("Table created")
#提交更改
conn.commit()
#关闭连接
conn.close()

2.主程序

from flask import *
import sqlite3 as sql
app = Flask(__name__)


@app.route("/")
def index():
    return render_template("index.html")

@app.route("/login")
def login():
    return render_template("login.html")
# @app.route("/login_check")
# def login_check():


@app.route("/register",methods=["GET","POST"])
def register():
    if request.method == "POST":
        try:
            username = request.form["username"]
            number = request.form["number"]
            college = request.form["college"]
            major = request.form["major"]
            password = request.form["password"]
            password2 = request.form["password2"]
            if password != password2:
                msg = "密码不一致"
                return render_template("result.html",msg=msg)
            else:
                with sql.connect("stu.db") as conn:
                    cur = conn.cursor()
                    cur.execute("INSERT INTO students(username,number,college,major,password) VALUES(?,?,?,?,?)",(username,number,college,major,password))
                    conn.commit()
                    conn.close()
                    msg = "注册成功"
        except:
            conn.rollback()
            msg = "注册失败"
        finally:
            return render_template("result.html",msg=msg)
    else:
        return render_template("register.html")






@app.route("/admin_login")
def admin_login():
    return render_template("admin_login.html")

@app.route("/list")
def list():
    return render_template("list.html")

if __name__ == "__main__":
    app.run(debug=True)

3.登录校验

4.注册校验

5.管理员界面

6.增删改查

 

[Python-Flask-SQLite]学生管理系统V1.0

标签:exe   with   rollback   port   ==   connect   admin   into   not   

原文地址:https://www.cnblogs.com/Skybiubiu/p/13291309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!