flask使用request获取表单提交数据和获取url

基本使用

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)

flask通过request获取url

1 request.path: /test/a
2 request.host: 127.0.0.1:5000
3 request.host_url: http://127.0.0.1:5000/
4 request.full_path: /test/a?x=1
5 request.script_root: 
6 request.url: http://127.0.0.1:5000/test/a?x=1
7 request.base_url: http://127.0.0.1:5000/test/a
8 request.url_root: http://127.0.0.1:5000/
    原文作者:菲宇
    原文地址: https://blog.csdn.net/bbwangj/article/details/85017353
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞