crm开发定制python Web开发 flask轻量级Web框架实战项目--实现功能--账号密码登录界面(连接数据库Mysql)

ps:crm开发定制各位好久不见,我回家了!crm开发定制终于有时间把之前的一crm开发定制些东西整理一下了(好吧,crm开发定制之前是我太懒了),crm开发定制今天分享一个功能简单的python web实战项目,crm开发定制后期功能可自行丰富。

先看效果

 

 输入正确用户名和密码即可登录成功(下图为存放数据的表单)

 

 正文


一、导入模块

  1. from flask import Flask, render_template, request
  2. import pymysql

二、创建web程序,连接数据库

  1. app = Flask(__name__)
  2. def get_conn():
  3. # 建立与mysql连接
  4. conn = pymysql.connect(host="localhost", user="root", password="root", db="tmp", charset="utf8")
  5. # c创建游标A
  6. cursor = conn.cursor()
  7. return conn, cursor

localhost: 本机数据库。

user:设置的数据库用户名。

password:数据库密码,,默认情况下是root,如果后期自己修改了则改为相应的即可。

db:所要连接的数据库名称。 

1.关闭模块(关闭数据库连接,关闭游标)

代码如下:

  1. def close_conn(conn, cursor): # 关闭模块
  2. if cursor:
  3. cursor.close()
  4. if conn:
  5. conn.close()

2.查询模块(用于从数据库中查询信息)

代码如下:

  1. def query(sql, *args): # 查询模块
  2. conn, cursor = get_conn()
  3. cursor.execute(sql, args)
  4. res = cursor.fetchall()
  5. conn.commit()
  6. close_conn(conn, cursor)
  7. return res
  8. def get_user(username, password): # 从数据库中查询用户名和密码
  9. sql = "select id from sys_user where username= '" + username + "' and password= '" + password + "'"
  10. res = query(sql)
  11. return res

sys_user:存放用户名和密码的表。


 三、写一个函数来处理浏览器发送过的请求,请求到/是自动执行这个函数。

  1. @app.route('/') # 必须加上路由,否则访问和函数没有关联,当访问到127.0.0.1:5000/,执行函数
  2. def index():
  3. return render_template('login.html')
  4. @app.route('/login', methods=['post'])
  5. def login():
  6. username = request.form.get('username') # 接收form表单传参
  7. password = request.form.get('password')
  8. res = get_user(username, password)
  9. if res:
  10. return render_template('xxx.html',msg='登陆成功')
  11. else:
  12. return render_template('login.html', msg='登录失败')

 登陆成功后进入xxx页面,登陆失败留在本页面并给出提示。

登陆界面为表单传参,这里用到POST请求。

在HTTP协议的请求类型里:

  • GET:从服务器端获取资源或数据

  • POST:向服务器端提交数据

GET请求发送数据的时候,一般会将请求数据放在url字符串中发送给服务器端,所以从安全性角度来看相对没有POST请求安全性高,所以GET请求一般不会用于比较隐私数据的传输,而POST请求是将请求数据放在请求body里面,所以一般用于表单数据,登陆数据等数据的传输。

四、启动应用程序

  1. if __name__ == '__main__': # 固定的写法,程序的入口
  2. app.run() # 启动应用程序,

 五、完整代码

  • myflask.py

  1. from flask import Flask, render_template, request
  2. import pymysql
  3. # 创建web应用程序
  4. app = Flask(__name__)
  5. def get_conn():
  6. # 建立与mysql连接
  7. conn = pymysql.connect(host="localhost", user="root", password="root", db="tmp", charset="utf8")
  8. # c创建游标A
  9. cursor = conn.cursor()
  10. return conn, cursor
  11. def close_conn(conn, cursor): # 关闭模块
  12. if cursor:
  13. cursor.close()
  14. if conn:
  15. conn.close()
  16. def query(sql, *args): # 查询模块
  17. conn, cursor = get_conn()
  18. cursor.execute(sql, args)
  19. res = cursor.fetchall()
  20. conn.commit()
  21. close_conn(conn, cursor)
  22. return res
  23. def get_user(username, password): # 从数据库中查询用户名和密码
  24. sql = "select id from sys_user where username= '" + username + "' and password= '" + password + "'"
  25. res = query(sql)
  26. return res
  27. # 写一个函数来处理浏览器发送过的请求,请求到/是自动执行这个函数
  28. @app.route('/') # 必须加上路由,否则访问和函数没有关联,当访问到127.0.0.1:5000/,执行函数
  29. def index():
  30. return render_template('login.html')
  31. @app.route('/login', methods=['post'])
  32. def login():
  33. username = request.form.get('username') # 接收form表单传参
  34. password = request.form.get('password')
  35. res = get_user(username, password)
  36. if res:
  37. return render_template('game.html',msg='登陆成功')
  38. else:
  39. return render_template('login.html', msg='登录失败')
  40. if __name__ == '__main__': # 固定的写法,程序的入口
  41. app.run() # 启动应用程序,

 

  • 前端页面代码 (需要可自行拿,记得点个赞谢啦)

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>登陆界面</title>
  6. <link rel="icon" href="http://v3.bootcss.com/favicon.ico">
  7. <style>
  8. li {
  9. list-style: none;
  10. }
  11. body {
  12. margin: 0;
  13. padding: 0;
  14. box-sizing: border-box;
  15. background-image: url(static/img/鹿3.jpg);
  16. /* background-image: url(/static/img/1.jpg); */
  17. background-repeat: no-repeat;
  18. /*这里的100% auto 表示将背景图片的长度100%显示,高度自适应*/
  19. background-size: 100% auto;
  20. }
  21. #maxbox {
  22. margin: 0 auto;
  23. margin-top: 200px;
  24. padding: 20px, 50px;
  25. /*这里的90表示以不透明度90%显示*/
  26. background-color: #00000090;
  27. text-align: center;
  28. width: 600px;
  29. height: 400px;
  30. border-radius: 10px;
  31. }
  32. #maxbox h1 {
  33. padding: 0;
  34. padding-top: 60px;
  35. color: white;
  36. font-size: 30px;
  37. padding-bottom: 4px;
  38. border-bottom: solid 1px white;
  39. }
  40. #maxbox h2 {
  41. font-weight: 700;
  42. color:white;
  43. }
  44. #maxbox .inputbox {
  45. margin-top: 30px;
  46. }
  47. #maxbox .inputText {
  48. margin-top: 20px;
  49. }
  50. #maxbox .inputText span {
  51. color: white;
  52. font-size: 18px;
  53. }
  54. #maxbox .inputText input {
  55. border: 0;
  56. padding: 6px;
  57. border-bottom: 1px solid white;
  58. /*这里的00表示不透明度为0,即透明显示*/
  59. background-color: #FFFFFF00;
  60. color: white;
  61. }
  62. #maxbox .inputbox .inputButton {
  63. margin: 0;
  64. border: 0;
  65. margin-top: 20px;
  66. width: 145px;
  67. height: 25px;
  68. /*给这个按钮变为圆角边角*/
  69. border-radius: 25px;
  70. color: white;
  71. background-color: #3498db;
  72. }
  73. #sign_up {
  74. margin-top: 50px;
  75. color: white;
  76. font-size: 17px;
  77. }
  78. #sign_up a {
  79. color: #3498db;
  80. }
  81. </style>
  82. </head>
  83. <body>
  84. <div id="maxbox">
  85. <h1>登陆界面</h1>
  86. <h2>请登录</h2>
  87. <div class="inputbox">
  88. <!-- <form name="frm" action="" method="post">-->
  89. <form action="/login" method="post">-->
  90. <div class="inputText">
  91. <span class="iconfont icon-mine"></span>
  92. <input class="username" type="text" placeholder="用户名" name="username" style="color:while" />
  93. </div>
  94. <div class="inputText">
  95. <span class="iconfont icon-lock"></span>
  96. <input type="password" placeholder="密码" name="password" style="color:white" />
  97. <br>
  98. <input class="remember" name="remember" type="checkbox" value="" checked="checked">
  99. <span style="color:rgb(255, 255, 255)">记住我</span>
  100. </div>
  101. <input class="inputButton" type="submit" value="登录" />
  102. {{msg}}
  103. </form>
  104. </div>
  105. </div>
  106. </body>
  107. </html>

总结

以上就是今天要分享的内容,python+web+轻量级框架的实战小项目。后续功能可以自行丰富,原理都是类似的。

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发