python flask实现简单的用户管理系统

python3.7.4+mysql环境下,简单的用户管理

#app.py


from flask import Flask,render_template,request

from pymysql import connect

app = Flask(__name__)



@app.route('/')
def home():
    return render_template('home.html')

@app.route('/addPerson')
def addPerson():
    return render_template('addPerson.html')

@app.route('/commit',methods = ['POST','GET'])
def commit():
    if request.method == 'POST':
        print('进入post')
        try:
            name = request.form['studentName']
            city = request.form['city']
            address = request.form['address']
            phone = request.form['phone']
            con = connect('localhost', 'root', '123456', 'test1')
            cur = con.cursor()
            #经过多次实验,这个语句才最适合现在的python+mysql
            sql = "INSERT INTO myfirst(name,city,address,phonenumber)  VALUES  ('{}','{}','{}','{}')".format(name,city,address,phone)
            print(sql)
            cur.execute(sql)
            con.commit()
            msg = "添加成功"

        except Exception as e:
            con.roolback()
            msg = "添加失败"
            print(msg)
        finally:
            con.close()
            print(msg)
            return render_template('result.html',message = msg)
            

@app.route('/lookup')
def lookup():
    con = connect('localhost', 'root', '123456', 'test1')
    cur = con.cursor()
    cur.execute("select * from myfirst")
    rows = cur.fetchall()
    con.close()
    return render_template("list.html", rows=rows)

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




    addPerson


    

填写学生信息


name

address

city

phone





    home


    
    
        






    


    

学生信息表

{%for row in rows%} {%endfor%}
{{row[0]}} {{row[1]}} {{row[2]}} {{row[3]}} {{row[4]}}
Go back to home page




    添加结果


    

添加结果是:{{message}}


返回主页

你可能感兴趣的:(python flask实现简单的用户管理系统)