• Flask学习笔记(九)


    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


    flask框架使用mysql实现用户注册登录系统

    通过前端(index.html,regist.html)和后端部分运行程序默认进入登录界面通过输入注册部分的路由进行注册
    前端部分:

    <!DOCTYPE html>
     <html>
     <head>
         <meta  charset="UTF-8">
         <title>用户登录</title>
     </head>
     <body>
         <h1>登录</h1>
         <form  method="post">
             <input type="text"  name="user" >
             <input  type="password" name="pwd" >
             <input  type="submit" name="登录">{{msg}}
         </form>
     </body>
     </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    导入扩展:

    import pymysql
    from flask import Flask
    from flask import render_template
    from flask import request
    import traceback
    import win32api,win32con
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    登录注册部分路由的设置

    @app.route('/')//默认登录界面
    def login():
        return render_template('login.html')
    @app.route('/regist')//访问注册
    def regist():
        return render_template('regist.html')
    @app.route('/registuser')//获取注册信息储存到数据库里
    def getRigistRequest():
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    连接数据库导入提前创建的数据库demo

    db = pymysql.connect(host="localhost", user="root", password="20220821", database="demo",charset="utf8")
    
    • 1

    使用cursor()方法获取操作游标

    cursor = db.cursor()
    
    • 1

    获取输入内容

    username=request.args.get('user')
    password=request.args.get('password')
    password2=request.args.get('password2')
    print(password)
    print(password2)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    通过对比pw是否等于pw2判断注册密码是否可行并导入数据库

    if password==password2:
            sql = "INSERT INTO user(user, password) VALUES ("+username+", "+password+")"
            try:
                # 执行sql语句
                cursor.execute(sql)
                db.commit()
                 #注册成功之后跳转到登录页面
                return render_template('login.html')
            except:
                #抛出错误信息
                traceback.print_exc()
                # 如果发生错误则回滚
                db.rollback()
                return '注册失败'
            # 关闭数据库连接
            db.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    两次密码输入错误后

      else:
            win32api.MessageBox(0,"两次输入密码不一致","提醒",win32con.MB_ICONWARNING)
            return render_template('regist.html')//返回注册页面
    
    • 1
    • 2
    • 3

    登录部分 # 连接数据库,此前在数据库中创建数据库test查询是否存在

    @app.route('/login')
    def login():
     db = pymysql.connect(host="localhost", user="root", password="20220821", database="flask",charset="utf8")
    
    • 1
    • 2
    • 3

    使用cursor()方法获取操作游标后通过select()查询语句

      cursor = db.cursor()
      sql = "select * from user where user=" + request.args.get('user') + " and password=" + request.args.get(
            'password') + ""
        try:
            cursor.execute(sql)
            results = cursor.fetchall()
            print(len(results))
            if len(results) == 1:
                return '登录成功'
        else:
                return '用户名或密码不正确'
                db.commit()
        except:
            traceback.print_exc()
            db.rollback()
        db.close()
        if __name__ == '__main__':
        app.run(debug=True)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    基于TCP的移动端IM即时通讯开发仍然需要心跳保活
    nodejs+vue+elementui线上买菜系统
    渗透测试-apt攻击与防御系列-利用WinRAR跨目录获取Net-NTLM Hash和DLL劫持
    LeetCode220807_73、旋转图像
    十、JVM常用启动参数
    英文科技论文写作与发表-写作实践(第7章)
    ​LeetCode解法汇总2651. 计算列车到站时间
    C语言,求自幂数
    智能指针一些实现分析
    Leetcode刷题详解——点名
  • 原文地址:https://blog.csdn.net/weixin_62115589/article/details/126457053