Flask+MySQL 实现新增用户并展示

发布于:2025-02-12 ⋅ 阅读:(16) ⋅ 点赞:(0)

1.app.py

from flask import Flask,render_template,request
import pymysql

app=Flask(__name__)


@app.route('/add/user',methods=['GET', 'POST'])
def register():
    if request.method == "GET":
        return render_template('add_user.html')

    print(request.form)

    username = request.form.get("user")
    password = request.form.get("pwd")
    mobile = request.form.get("mobile")

    # 1.连接MySQL
    conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="123456", 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 "添加成功"

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

    # 查询所有数据
    sql = "SELECT * FROM admin"
    cursor.execute(sql)
    data_list = cursor.fetchall()  # 获取所有记录

    # 关闭连接
    cursor.close()
    conn.close()

    return render_template('show_user.html', users=data_list)


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

2.在templates目录下创建两个html文件(这个文件夹名字是固定的)

add_user.html:

<!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="user" placeholder="用户名">
        <input type="text" name="pwd" placeholder="密码">
        <input type="text" name="mobile" placeholder="手机号">
        <input type="submit" value="提 交">
    </form>

</body>
</html>

show_user.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        a {
            display: inline-block;
            margin-top: 20px;
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <h1>用户列表</h1>

    <table>
        <tr>
            <th>ID</th>
            <th>用户名</th>
            <th>密码</th>
            <th>手机号</th>
        </tr>
        {% for user in users %}
        <tr>
            <td>{
  
  { user.id }}</td>
            <td>{
  
  { user.username }}</td>
            <td>{
  
  { user.password }}</td>
            <td>{
  
  { user.mobile }}</td>
        </tr>
        {% endfor %}
    </table>

    <a href="/add/user">添加新用户</a>
</body>
</html>

3.实现效果

点击“添加新用户”

提交:

查看“http://127.0.0.1:5000/show/user”,添加成功

学习:【最新Python的web开发全家桶(django+前端+数据库)-哔哩哔哩】 https://b23.tv/1iE0IVw