首页 > 其他分享 >FLASK

FLASK

时间:2022-10-24 12:23:05浏览次数:44  
标签:__ FLASK cursor item mobile password conn

1.增加用户

 

后台端口


from flask import Flask, render_template, request
import pymysql

app = Flask(__name__)


@app.route("/adduser", methods=["GET", "POST"])
def adduser():
if request.method == "GET":
return render_template("adduser.html")

username = request.form.get("user")
password = request.form.get("pwd")
mobile = request.form.get("mobile")
print(request.form)
# 1.连接数据库
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="", charset="utf8", db="unicom")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

# 2.发送命令
sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"
cursor.execute(sql, [username, password, mobile])
conn.commit()
# 3.关闭
cursor.close()
conn.close()
return "注册成功"



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

前端

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>用户注册</h1>
<form method="post" action="/adduser">
    <input type="text" name="user" placeholder="用户名">
    <input type="text" name="pwd" placeholder="密码">
    <input type="text" name="mobile" placeholder="手机号">
    <input type="submit" value="提 交">
</form>

 查

@app.route('/showuser')
def showuser():
    # 1.连接数据库
    conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="7", charset="utf8", db="unicom")
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    # 2.发送命令
    sql = "select * from admin"
    cursor.execute(sql)
    data_list = cursor.fetchall()
    return render_template("showuser.html",data_list=data_list)

    # 3.关闭
    cursor.close()
    conn.close()
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
</head>
<body>
<h1>用户列表</h1>
<table border="1">
    <thead>
        <tr>
            <th>ID</th>
            <th>用户名</th>
            <th>密码</th>
            <th>手机号</th>
        </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,cursor,item,mobile,password,conn
From: https://www.cnblogs.com/doctorsong/p/16820454.html

相关文章