• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pythonFlask+前端+mysql结合起来

武飞扬头像
小袁一定能学好Python
帮助1

 其中代码段coon里面的db换成自己的数据库的名字,host和passed换成自己mysql的用户和密码。

flask代码如下:

  1.  
    from flask import Flask, render_template ,request
  2.  
    import pymysql
  3.  
    app = Flask(__name__)
  4.  
     
  5.  
    @app.route('/', methods = ["GET", "POST"])
  6.  
    def index():
  7.  
    if request.method == "GET":
  8.  
    return render_template('index.html')
  9.  
     
  10.  
    username = request.form.get("user")
  11.  
    password = request.form.get("password")
  12.  
    mobile = request.form.get("mobile")
  13.  
    print(username,password,mobile)
  14.  
     
  15.  
    #连接mysql
  16.  
    conn = pymysql.connect(host="localhost", user='root', passwd="Mysql123", charset='utf8', db='unicom')
  17.  
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
  18.  
    #发送命令
  19.  
    sql = "insert into admin(username, password, mobile) values(%s, %s, %s)"
  20.  
    cursor.execute(sql, [username, password, mobile])
  21.  
    conn.commit()
  22.  
    #关闭
  23.  
    cursor.close()
  24.  
    conn.close()
  25.  
     
  26.  
    return 'thank you uesing'
  27.  
     
  28.  
    if __name__ == '__main__':
  29.  
    app.run()
学新通

html代码如下:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>Title</title>
  6.  
    </head>
  7.  
    <body>
  8.  
    <h1>添加用户</h1>
  9.  
    <form method="post">
  10.  
    <input type="text" name="user" placeholder="用户名">
  11.  
    <input type="text" name="password" placeholder="密码">
  12.  
    <input type="text" name="mobile" placeholder="手机号">
  13.  
    <input type="submit" value="提交">
  14.  
    </form>
  15.  
     
  16.  
     
  17.  
     
  18.  
     
  19.  
    </body>
  20.  
    </html>
学新通

运行然后打开网站:学新通

 接着输入数据提交:学新通

学新通 

然后去数据库查看有没有添加成功:学新通 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfhjbi
系列文章
更多 icon
同类精品
更多 icon
继续加载