zoukankan      html  css  js  c++  java
  • 08flask中get和post请求。

    1,get请求。

      使用场景:获取信息并没有对服务器的数据或者资源进行修改,则用get。

      传参:get请求传参是放在URL中,通过“?”的形式指定键值对。

    2,post请求。

      使用场景:对服务器产生影响,则用post。

      传参:post不是放在URL中,而是“form data”的形式发送给服务器。

    3,使用:

    @app.route('/')
    def hello_world():
        return render_template('index.html')
    
    @app.route('/search/')
    def search():
        return 'search!'
    对应的index页面为:
    <a href="{{ url_for("search",q = "hello") }}">搜寻关键字为hello</a>

    4,获取get请求中的关键字。

    @app.route('/')
    def hello_world():
    
        return render_template('index.html')
    
    # 获取用户提交的关键字
    @app.route('/search/')
    def search():
        haha = request.args.get('q')
        print(haha)
        return '用户提交的关键字是:%s' %haha
    
    <a href="{{ url_for("search",q = "hello") }}">搜寻</a>

    5,提交并获取post请求中的关键字。

    G:Flaskget_postapp.py
    # post请求与获取提交的关键字
    @app.route('/login/',methods=["POST","GET"])
    def login():
    if request.method == "GET":
    return render_template('login.html')
    else:
    username = request.form.get('username')
    password = request.form.get('password')
    return "username:%s /n password:%s" %(username,password)

    login.html
    <form action="{{ url_for('login') }}" method="post">
    <input type="text" name="username" placeholder="请输入用户名"><br>
    <input type="password" name="password" placeholder="请输入密码"><br>
    <input type="submit" name="username" value="登录">

    </form>

      

  • 相关阅读:
    cocoapods 命令
    开发常用
    ios 定位
    LoadingView
    自定义cell右侧 多按钮
    cocoaPods
    AFNetWorking
    iphone自定义铃声
    升级为iOS9后,默认请求类型为https,如何使用http进行请求会报错(引用他人的)
    理解c语言中的指针
  • 原文地址:https://www.cnblogs.com/two-peanuts/p/10941795.html
Copyright © 2011-2022 走看看