Python flask request

时间:2024-03-23 18:38:18
from flask import Flask, render_template, request

app = Flask(__name__)


@app.route('/index', methods=['GET', "POST"])
def index():
    if request.method == "GET":
        return render_template("index.html")
    if request.method == "POST":
        return 'this is post'


if __name__ == '__main__':
    app.run(debug=True)

第一。获取get和post请求 如代码所示

第二 获取数值

from flask import Flask, render_template, request

app = Flask(__name__)


@app.route('/index', methods=['GET', "POST"])
def index():
    if request.method == "GET":
        return render_template("index.html")
    if request.method == "POST":
        name = request.form.get("name")
        password = request.form.get("password")
        print(name, password)
        return 'this is post'


if __name__ == '__main__':
    app.run(debug=True)

h5代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="post">
    账号:
    <input type="text" name="name">
    <br>
    密码:
    <input type="password" name="password">
    <br>
    <input type="submit" name="submit">
</form>

</body>
</html>