2.flask使用request獲取表單提交數據


基本使用

web開發免不了需要獲取用戶提交的數據,Flask為我們提供了request對象來獲取用戶提交給服務器的數據。
下面是一個最基本的獲取數據的例子:

templates文件夾下的login.html文件中添加如下代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action="#" method="post">
        <p>提交:<input type="text" name="username" ></p>
        <p>密碼:<input type="password" name="password" ></p>
        <button type="submit">提交</button>
    </form>
</body>
</html>

app.py文件中添加如下代碼:

form flask improt Flask, render_template, request

app = Flask(__name__)

# 配置路由,獲取用戶提交的登錄信息

# 指定請求方式,如果不指定,則無法匹配到請求
@app.route("/login", methods=("GET", "POST"))
def login():
  # GET請求
  if request.method == "GET":
    return render_template("login.html")
  # POST請求
  if request.method == "POST":
    
    print(request.headers)
    print(request.json)
    print(request.data)
    # 獲取數據並轉化成字典
    user_info = request.form.to_dict()
    if user_info.get("username") == "admin" and user_info.get("password") == '123456':
      return redirect("/")
  print(request.form.to_dict())
  # args 獲取地址欄的hash值
  print(request.args.to_dict())
  return "用戶名密碼錯誤"

app.run(host="0.0.0.0", port=3000, debug=True)

上面代碼實現了一個簡單的用戶登錄。

獲取全部參數

request對象提供了values屬性來獲取表單提交的全部數據,我們在app.py中添加request.values

form flask improt Flask, render_template, request

app = Flask(__name__)

# 配置路由,獲取用戶提交的登錄信息

# 指定請求方式,如果不指定,則無法匹配到請求
@app.route("/login", methods=("GET", "POST"))
def login():
  # GET請求
  if request.method == "GET":
    return render_template("login.html")
  # POST請求
  if request.method == "POST":
    
    # request.values獲取數據並轉化成字典
    user_info = request.values.to_dict();
    
    if user_info.get("username") == "admin" and user_info.get("password") == '123456':
      return redirect("/")
      
  return "用戶名密碼錯誤"

app.run(host="0.0.0.0", port=3000, debug=True)

文件上傳

使用request實現圖片上傳非常簡單,只需要兩行代碼即可。
下面我們修改login.html文件,來實現一個簡單的圖片上傳。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action="#" method="post" enctype="multipart/form-data">
        <p>提交:<input type="text" name="username" ></p>
        <p>密碼:<input type="password" name="password" ></p>
        <p>文件:<input type="file" name="myFiles"></p>
        <button type="submit">提交</button>
    </form>
</body>
</html>

app.py進行如下修改:

from flask import Flask, render_template, request

app = Flask(__name__)


@app.route("/")
def index():
    return "<h1>Flask</h1>"

@app.route("/login", methods=("GET", "POST"))
def login():
  if request.method == "GET":
    return render_template("login.html")

  if request.method == "POST":

    # 獲取上傳圖片數據
    tupian = request.files.get('myFiles')
    # 保存圖片到根目錄
    tupian.save(tupian.filename)
    
    user_info = request.form.to_dict()
    if user_info.get("username") == "admin" and user_info.get("password") == '123456':

      return redirect("/")
  return "用戶名密碼錯誤"

app.run(host="0.0.0.0", port=9527, debug=True)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM