python flask實(shí)現(xiàn)簡(jiǎn)單的用戶管理系統(tǒng)

python3.7.4+mysql環(huán)境下,簡(jiǎn)單的用戶管理

#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('進(jìn)入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()
            #經(jīng)過(guò)多次實(shí)驗(yàn),這個(gè)語(yǔ)句才最適合現(xiàn)在的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.html -->
<!DOCTYPE html>
<html>
<head>
    <title>addPerson</title>
</head>
<body>
    <form action="/commit" method="POST">

        <h3>填寫(xiě)學(xué)生信息</h3>
        <br>
        name<br>
        <input type="text" name="studentName">
        <br>
        address<br>
        <textarea name="address"></textarea>
        <br>
        city<br>
        <input type="text" name="city">
        <br>
        phone<br>
        <input type="text" name="phone">
        <br>
        <input type="submit" value="submit">

    </form>

</body>
</html>
<!-- home.html -->
<!DOCTYPE html>
<html>
<head>
    <title>home</title>
</head>
<body>
    <div>
        <a href="/addPerson">新增用戶</a>
    </div>
    <div>
        <a href="/lookup">查詢用戶</a>
    </div>
        
</body>
</html>
<!-- list.html -->
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>學(xué)生信息表</h1>
    <table border = 1>
        {%for row in rows%}
            <tr>
                <td>{{row[0]}}</td>
                <td>{{row[1]}}</td>
                <td>{{row[2]}}</td>
                <td>{{row[3]}}</td>
                <td>{{row[4]}}</td>
            </tr>
        {%endfor%}
    </table>
    <a href = "/">Go back to home page</a>
</body>
</html>
<!-- result.html -->
<!DOCTYPE html>
<html>
<head>
    <title>添加結(jié)果</title>
</head>
<body>
    <h1>添加結(jié)果是:{{message}}</h1>
    <br>
    <a href="/">返回主頁(yè)</a>
</body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 什么是數(shù)據(jù)庫(kù)? 數(shù)據(jù)庫(kù)是存儲(chǔ)數(shù)據(jù)的集合的單獨(dú)的應(yīng)用程序。每個(gè)數(shù)據(jù)庫(kù)具有一個(gè)或多個(gè)不同的API,用于創(chuàng)建,訪問(wèn),管理...
    chen_000閱讀 4,138評(píng)論 0 19
  • 今天看到一位朋友寫(xiě)的mysql筆記總結(jié),覺(jué)得寫(xiě)的很詳細(xì)很用心,這里轉(zhuǎn)載一下,供大家參考下,也希望大家能關(guān)注他原文地...
    信仰與初衷閱讀 4,826評(píng)論 0 30
  • 安裝并配置MySQL數(shù)據(jù)庫(kù) 3.2.1安裝MySQL數(shù)據(jù)庫(kù) 1.MySQL數(shù)據(jù)庫(kù)的安裝環(huán)境準(zhǔn)備 如果讀者沒(méi)有物理服...
    SkTj閱讀 4,196評(píng)論 0 6
  • 背景: 閱讀新聞 11.2.0.3 ASM實(shí)例出現(xiàn)ORA-4031錯(cuò)誤導(dǎo)致數(shù)據(jù)庫(kù)歸檔失敗 [日期:2014-03-...
    kuangjiao223閱讀 961評(píng)論 0 0
  • 一、Python簡(jiǎn)介和環(huán)境搭建以及pip的安裝 4課時(shí)實(shí)驗(yàn)課主要內(nèi)容 【Python簡(jiǎn)介】: Python 是一個(gè)...
    _小老虎_閱讀 6,319評(píng)論 0 10

友情鏈接更多精彩內(nèi)容