美文网首页
python flask接收前端post请求

python flask接收前端post请求

作者: 灰太狼256 | 来源:发表于2019-04-29 17:31 被阅读0次

    html部分,保存文件名称为index.html

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>title</title>
        <style type="text/css"> 
        </style>
    </head>
    <body>
        <form id="form" name="form" action="http://localhost:5000/" method="post">
            <input name="name" value="joke">
            <input type="submit" value="提交">
        </form>
    </body>
    </html>
    

    python部分,保存文件名称为request.py

    from flask import Flask,request,render_template
    
    app = Flask(__name__)
     
    @app.route('/', methods=['GET', 'POST'])
    def req():
        if request.method == 'POST':
            name = request.form.get('name')
            print(name)
            return 'This is a' + ' ' + name
        else:
            return render_template('index.html') 
    
    if __name__=="__main__":
        app.run(debug=True)
    
    第一步:

    在存放 request.py 的目录下输入命令:
    C:\Users\John\Desktop\py>python request.py
    当你看到Running on http://127.0.0.1:5000/ ...时,表示运行成功,不要关闭命令行窗口,最小化放到后台运行即可:

    image.png
    第二步:

    打开游览器输入localhost:5000127.0.0.1:5000你会看到:

    image.png
    第三步:

    点击提交你会看到浏览器输出This is a joke

    image.png

    以及你在命令窗里会看到jokePOST请求:

    image.png

    移步本人博客

    相关文章

      网友评论

          本文标题:python flask接收前端post请求

          本文链接:https://www.haomeiwen.com/subject/prvynqtx.html