Python笔记-Flask框架Get和Post参数相关
程序员文章站
2023-12-27 08:53:46
...
程序结构如下:
源码如下:
application.py
from flask import Flask
from controller import index_page
app = Flask(__name__)
app.register_blueprint(index_page, url_prefix = "/it1995")
if __name__ == "__main__":
app.run(host = "0.0.0.0", debug = True)
controller.py
from flask import Flask, Blueprint, request
index_page = Blueprint("index_page", __name__)
@index_page.route("/")
def index_page_index():
return "index_page"
@index_page.route("/my")
def hell():
return "Hello World"
@index_page.route("/get")
def get():
req = request.values
var_a = req["a"] if "a" in req else "Hello"
return "request:%s, params: %s, var_a: %s" % (request.method, request.args, var_a)
@index_page.route("/post", methods = ["POST"])
def post():
req = request.values
var_a = req["a"] if "a" in req else "Hello"
return "request:%s, params: %s, var_a: %s" % (request.method, request.args, var_a)
@index_page.route("/upload", methods = ["POST"])
def upload():
f = request.files["file"] if "file" in request.files else None
return "request:%s, params: %s, file: %s" % (request.method, request.files, f)
这里有两点要注意:
一个是上传文件:
另外一个是在post方法中当url含有a=bget,在body中a=bpost如下:
从中可以看到这个有个坑在post中,如果有url中含有相同的参数,会使用url上的。
这点要注意下。