运维开发网

Flask request 请求参数管理

运维开发网 https://www.qedev.com 2020-09-14 12:26 出处:51CTO 作者:breaklinux
1.Flask request 请求参数管理 post请求参数接收

1.为什么要学习Flask request 请求参数管理

1.一个完整的系统组成分为两部分(后端api,前端页面)
* 前端页面负责用户体验交互式效果--将用户输入的数据传递给后端api进行计算和处理.
* 后端api负责业务模块功能实现-将预期的结果返回给前端页面

**2.使用Bootstrap前端框架模拟form表单数据提交

 <!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
   <href="./bootstrap-datetimepicker/dist/css/bootstrap-datepicker3.min.css">
  </head>
  <body>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
   <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/usm/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> 

    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<form enctype="multipart/form-data" action="http://192.168.31.202:5050/Login" method="post">
    <div class="form-group col-sm-4">
      <label for="inputPassword4">登陆用户</label>
      <input type="text" name="name" class="form-control" id="inputPassword4">
  </div>
   <div class="form-group col-sm-4">
     <label for="inputAddress">登陆密码</label>
     <input type="password" name="passwd" class="form-control" id="inputAddress" placeholder="">
    </div>

  <button type="submit" name="submit" class="btn btn-primary">Sign in</button>
</form>
  </body>

</html>

3.flask 接收Bootstrap传递form表单数据

from flask import Flask, render_template, request,Response
import os
import json
app = Flask(__name__)

@app.route('/login')
def upload_file():
   return render_template('index.html')

@app.route('/Login', methods = ['GET', 'POST'])
def uploader():
   if request.method == 'GET':
      return Response(json.dumps({"code": 0, "data": "test"}), mimetype='application/json')

   elif request.method == 'POST':
        name = request.form['name']
        passwd = request.form['passwd']
        if name and passwd:
           return Response(json.dumps({"code": 0, "data": "user login success"}), mimetype='application/json') 
        else:
           return Response(json.dumps({"code": 0, "data": "账号或密码未输入 "}), mimetype='application/json')     
if __name__ == '__main__':
    app.run(
        host="0.0.0.0",
        port=5050,
        debug=True
    )

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号