国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

flask結(jié)合mysql實(shí)現(xiàn)用戶的添加和獲取

這篇具有很好參考價(jià)值的文章主要介紹了flask結(jié)合mysql實(shí)現(xiàn)用戶的添加和獲取。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

1、數(shù)據(jù)庫(kù)準(zhǔn)備

已經(jīng)安裝好數(shù)據(jù)庫(kù),并且創(chuàng)建數(shù)據(jù)庫(kù)和表

create database unicom DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
CREATE TABLE admin(
id int not null auto_increment primary key,
username VARCHAR(16) not null,
password VARCHAR(64) not null,
mobile VARCHAR(11) not null

);

2、新增用戶

通過(guò)flask 實(shí)現(xiàn)一個(gè)get方法去獲取用戶添加的頁(yè)面,再實(shí)現(xiàn)一個(gè)post方法去提交用戶輸入的信息到數(shù)據(jù)庫(kù)。這兩個(gè)方法是可以通過(guò)一個(gè)頁(yè)面來(lái)實(shí)現(xiàn)的。

  • 實(shí)現(xiàn)一個(gè)用戶添加的html頁(yè)面,add_user.html, 在flask中這個(gè)html 需要存放在根目錄的templates目錄下
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>添加用戶</h1>
<form method="post" action="/add/user">
    <input type="text" name="username" placeholder="用戶名">
    <input type="text" name="passwd" placeholder="密碼">
    <input type="text" name="mobile" placeholder="電話">
    <input type="submit" value="提交">
</form>

</body>
</html>
  • 在項(xiàng)目根目錄實(shí)現(xiàn)app.py的主程序
    get和post都指向的是/add/user路徑,如果是get方法就直接返回add_user.html頁(yè)面,如果是post 請(qǐng)求,就先通過(guò)request.form.get獲取到用戶輸入的數(shù)據(jù),再連接數(shù)據(jù)庫(kù),通過(guò)執(zhí)行插入語(yǔ)句將用戶輸入的內(nèi)容插入到數(shù)據(jù)庫(kù)。
from flask import Flask, render_template,request
import pymysql

app = Flask(__name__)

@app.route('/add/user', methods=['GET','POST'])
def add_user():
    if request.method == 'GET':
        return render_template('/add_user.html')
    else:
        username = request.form.get('username')
        passwd = request.form.get('passwd')
        mobile = request.form.get('mobile')
        print(username,passwd,mobile)
        #添加到數(shù)據(jù)庫(kù)
        conn = pymysql.connect(host="43.252.4.131", port=3306, user="root", passwd="123456", charset='utf8',db='unicom')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"
        cursor.execute(sql, [username, passwd, mobile])
        conn.commit()

        cursor.close()
        conn.close()

        return "添加成功"

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

  • 執(zhí)行效果
    運(yùn)行app.pyflask結(jié)合mysql實(shí)現(xiàn)用戶的添加和獲取,python,flask,mysql
    在瀏覽器訪問(wèn):http://127.0.0.1:5000/add/user
    flask結(jié)合mysql實(shí)現(xiàn)用戶的添加和獲取,python,flask,mysql
    再查看數(shù)據(jù)庫(kù),數(shù)據(jù)已添加到數(shù)據(jù)庫(kù)
    flask結(jié)合mysql實(shí)現(xiàn)用戶的添加和獲取,python,flask,mysql
    這一個(gè)一個(gè)添加用戶的簡(jiǎn)單功能就實(shí)現(xiàn)了。

3、查詢用戶數(shù)據(jù)

  • 在app.py中需要增加一個(gè)/show/user的方法,這個(gè)方法就是是數(shù)據(jù)庫(kù)獲取數(shù)據(jù),再展示到瀏覽器,代碼如下:
from flask import Flask, render_template,request
import pymysql

app = Flask(__name__)

@app.route('/add/user', methods=['GET','POST'])
def add_user():
    if request.method == 'GET':
        return render_template('/add_user.html')
    else:
        username = request.form.get('username')
        passwd = request.form.get('passwd')
        mobile = request.form.get('mobile')
        print(username,passwd,mobile)
        #添加到數(shù)據(jù)庫(kù)
        conn = pymysql.connect(host="43.252.4.131", port=3306, user="root", passwd="123456", charset='utf8',db='unicom')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"
        cursor.execute(sql, [username, passwd, mobile])
        conn.commit()

        cursor.close()
        conn.close()

        return "添加成功"

@app.route('/show/user')
def show_user():
    #從數(shù)據(jù)庫(kù)獲取數(shù)據(jù)
    conn = pymysql.connect(host="43.254.3.133", port=5001, user="root", passwd="Mysql@si20230206_e", charset='utf8',db='unicom')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    sql = "select * from admin"
    cursor.execute(sql)
    data_list = cursor.fetchall()    #獲取所有數(shù)據(jù)

    cursor.close()
    conn.close()
    return render_template('/get_user.html',data_list=data_list)

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

data_list 就是從數(shù)據(jù)庫(kù)獲取到的數(shù)據(jù),是一個(gè)列表套字典的數(shù)據(jù)。

  • 獲取到的數(shù)據(jù)要通過(guò)get_user.html來(lái)展示,get_user.html中就可以通過(guò)一個(gè)表格來(lái)顯示,并且數(shù)據(jù)是需要?jiǎng)討B(tài)來(lái)顯示的。get_user.html的代碼如下
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h1>用戶列表</h1>
       <table border="1">
           <thead>
           <tr>
               <td>ID</td>
               <td>用戶名</td>
               <td>秘密</td>
               <td>手機(jī)</td>
           </tr>
           </thead>
           <tbody>
           {% for item in data_list %}
                <tr>
                    <td>{{item.id}}</td>
                    <td>{{item.username}}</td>
                    <td>{{item.password}}</td>
                    <td>{{item.mobile}}</td>
                </tr>
           {% endfor %}
           </tbody>
       </table>
</body>
</html>

在flask 中可以將數(shù)據(jù)作為參數(shù)傳到前端頁(yè)面

return render_template(‘/get_user.html’,data_list=data_list)
1、找到get_user.html的文件,讀取所有的內(nèi)容
2、找到內(nèi)容中特殊的占位符,將數(shù)據(jù)替換
3、將替換完的字符串返回給用的瀏覽器

<tbody>
{% for item in data_list %}
     <tr>
         <td>{{item.id}}</td>
         <td>{{item.username}}</td>
         <td>{{item.password}}</td>
         <td>{{item.mobile}}</td>
     </tr>
{% endfor %}
</tbody>

這里是flask框架提供的方法,通過(guò)特殊占位符來(lái)實(shí)現(xiàn)for循環(huán),可以將傳過(guò)來(lái)的參數(shù)循環(huán)顯示,比如下面的item 就是data_list中的一個(gè)值,在td中再通過(guò)item的key 獲取對(duì)應(yīng)的字段

  • 訪問(wèn)http://127.0.0.1:5000/show/user 頁(yè)面效果,數(shù)據(jù)庫(kù)的數(shù)據(jù)都拿過(guò)來(lái)了:
    flask結(jié)合mysql實(shí)現(xiàn)用戶的添加和獲取,python,flask,mysql
  • 表格比較簡(jiǎn)陋,還可以通過(guò)引入bootstrap美化一下表格
    在static目錄下引入bootstarp
    flask結(jié)合mysql實(shí)現(xiàn)用戶的添加和獲取,python,flask,mysql
    在get_user.html中引入,在head中引入css,在body中引入js
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/plugins/bootstrap-3.4.1/css/bootstrap.css">
</head>
<body>
<script src="/static/js/jquery-3.7.0.min.js"></script>
<script src="/static/plugins/bootstrap-3.4.1/js/bootstarp.js"></script>
</body>
</html>

在表格中使用屬性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/plugins/bootstrap-3.4.1/css/bootstrap.css">
</head>
<body>
<div class="container">
   <h1>用戶列表</h1>
       <table class="table table-bordered">
           <thead>
           <tr>
               <td>ID</td>
               <td>用戶名</td>
               <td>秘密</td>
               <td>手機(jī)</td>
           </tr>
           </thead>
           <tbody>
           {% for item in data_list %}
                <tr>
                    <td>{{item.id}}</td>
                    <td>{{item.username}}</td>
                    <td>{{item.password}}</td>
                    <td>{{item.mobile}}</td>
                </tr>
           {% endfor %}
           </tbody>
       </table>
</div>
<script src="/static/js/jquery-3.7.0.min.js"></script>
<script src="/static/plugins/bootstrap-3.4.1/js/bootstarp.js"></script>
</body>
</html>

瀏覽器訪問(wèn)http://127.0.0.1:5000/show/user 展示效果:
flask結(jié)合mysql實(shí)現(xiàn)用戶的添加和獲取,python,flask,mysql文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-606748.html

到了這里,關(guān)于flask結(jié)合mysql實(shí)現(xiàn)用戶的添加和獲取的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包