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>

      

  • 相关阅读:
    Activiti系列——如何在eclipse中安装 Activiti Designer插件
    C语言 二维数组与指针笔记
    Ubuntu linux设置从当前目录下加载动态库so文件
    Ubuntu14.04安装nfs服务器
    Ubuntu14.04 搭建FTP服务器
    Linux备忘命令
    Java实现对xml文件的增删改查
    Java利用jacob实现打印Excel文件
    git操作列表
    swiper 窗口宽度变化,页面宽度高度变化 导致自动滑动 解决方案
  • 原文地址:https://www.cnblogs.com/two-peanuts/p/10941795.html
Copyright © 2011-2022 走看看