首页 👨‍💻计算机,🏂Flask

Flask-web-登录练习

前端 layui 后端:mysql+python

1,目录结构

image-20200930215512013

2,user.html文件代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="{{url_for('static',filename='layui/css/layui.css')}}" />
  <link rel="stylesheet" href="{{url_for('static',filename='css/index.css')}}" />
  <title>登录</title>
</head>
<body>
  <form id="myform" class="layui-form" method="POST">
    <div id="main">
      <div class="center">
     <h1>登录</h1>
    <div class="layui-form-item">
      <label class="layui-form-label"><i class="layui-icon layui-icon-username"></i>用户名</label>
      <div class="layui-input-inline ">
        <input type="text" id="userName" required  lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
      </div>
  
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label"><i class="layui-icon layui-icon-password"></i>密码框</label>
      <div class="layui-input-inline">
        <input type="password" id="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
      </div>
    
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn layui-bg-black" type="submit">登录</button>
     <button class="layui-btn layui-bg-black">注册</button>
      </div>
    </div>
  </div>
    </div>
  </form>
  <script src="{{url_for('static',filename='js/index.js')}}"></script>
  <script src="{{url_for('static',filename='layui/layui.js')}}"></script>
  <script src="{{url_for('static',filename='js/jquery-3.5.1.min.js')}}"></script> 
</body>
</html>

3,database.py

import pymysql
HOST="localhost"
USER="root"
PWD="123456"



class DB:
  def __init__(self):
      self.host=HOST
      self.user=USER
      self.pwd=PWD
  def connect(self,db):
    self.connect=pymysql.connect(host=self.host,
                                    user=self.user,
                                    password=self.pwd,db=db)
    self.cursor=self.connect.cursor()
    print('中秋国庆快乐')

if __name__ == "__main__":
  db=DB()
  db.connect("school")
  #查询
  sql="select * form students where username='rose'"
  sql="select*from students"
  result=db.cursor.execute(sql)
  print("查询结果:",result)
  for row in db.cursor.fetchall():
    print(row)

4,user.py

from flask import Flask,render_template,jsonify,request
from database import DB

# 创键Flask app
app = Flask(__name__)

@app.route("/")
def index():
    return render_template("user.html")

# 编写路由
@app.route("/api/login",methods=["POST"])
def login():
    userName=request.form.get("userName")
    password=request.form.get("password")
    db=DB()
    db.connect("school")
    sql="select * from students where userName='%s' and password='%s'" %(userName,password)
    count=db.cursor.execute(sql)
    return jsonify({"code":count})
# 启动服务
if __name__ == "__main__":
    app.run()

运行结果:
运行结果




文章评论

目录