乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      flask 中g(shù)et和post用法

       刮骨劍 2019-04-10
      get和post
      1、get請求:
              使用場景:如果只對服務(wù)器獲取數(shù)據(jù),并沒有對服務(wù)器產(chǎn)生任何影響,那么這時(shí)候使用get請求
              傳參:get請求傳參是放在url中,并且是通過?的形式來指定key和value的
      2、post請求:
              使用場景:如果要對服務(wù)器產(chǎn)生影響,那么使用post請求
              傳參:post請求傳參不是放在URL中,是通過form data 的形式發(fā)送給服務(wù)器的
      get 其他年輕是通過flask.request.args來獲取
      post請求是通過flask.request.form來獲取
      post請求在模板中要注意幾點(diǎn):
      *input 標(biāo)簽中,要寫name來表示這個(gè)value的key,方便后臺(tái)獲取
      *在寫form表單的時(shí)候,要指定method=‘post’,并且要指定action='/login/'  
      
      
      1、get實(shí)例:
      模板文件index.html
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>首頁</title>
      </head>
      <body>
      <!--一旦點(diǎn)擊連接就會(huì)訪問視圖函數(shù)search,通過視圖函數(shù)來反轉(zhuǎn)URL,URL的參數(shù)是q,值為hello:就會(huì)返回url:http://127.0.0.1:5000/search/?q=hello-->
          <a href="{{ url_for('search',q='hello') }}">跳轉(zhuǎn)到搜索頁面</a>
      </body>
      

      </html>

      get_demo.py文件:

      #encoding:utf-8
      from flask import Flask,render_template,request
      
      app = Flask(__name__)
      
      
      @app.route('/')
      def index(): #一訪問127.0.0.1:5000就會(huì)返回index模板中的鏈接”跳轉(zhuǎn)到搜索頁面”
          return render_template('index.html')
      @app.route('/search/')
      def search():
          #arguments
          print request.args #獲取所有參數(shù)
          print request.args.get('q') #或者參數(shù)為q的值
          return 'search'
      
      @app.route('/login/',methods=['GET','POST'])
      def login():
          if request.method == 'GET': #如果請求方法時(shí)GET,返回login.html模板頁面
              return render_template('login.html')
          else:
              username = request.form.get('username')
              password = request.form.get('password')
      
      
      if __name__ == '__main__':
          app.run()
      
      2、post實(shí)例


      login.html模板文件:

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>首頁</title>
      </head>
      <body>
          <form action="{{ url_for('login') }}" method="post">
              <table>
                  <tbody>
                      <tr>
                          <td>用戶名:</td>
                          <td><input type="text" placeholder="請輸入用戶名" name="username"></td>
                      </tr>
                      <tr>
                          <td>密碼:</td>
                          <td><input type="text" placeholder="請輸入密碼" name="password"></td>
                      </tr>
                      <tr>
                          <td></td>
                          <td><input type="submit" value="登陸"></td>
                      </tr>
                  </tbody>
      
              </table>
      
          </form>
      </body>
      </html>
      get_post_demo.py文件
      #encoding:utf-8
      from flask import Flask,render_template,request
      
      app = Flask(__name__)
      
      
      @app.route('/')
      def index(): #一訪問127.0.0.1:5000就會(huì)返回index模板中的鏈接”跳轉(zhuǎn)到搜索頁面”
          return render_template('index.html')
      @app.route('/search/')
      def search():
          #arguments
          print request.args #獲取所有參數(shù)
          print request.args.get('q') #或者參數(shù)為q的值
          return 'search'
      
      @app.route('/login/',methods=['GET','POST'])  #指定訪問頁面的方法
      def login():
          if request.method == 'GET': #如果請求方法時(shí)GET,返回login.html模板頁面
              return render_template('login.html')
          else:
              username = request.form.get('username')
              password = request.form.get('password')
              return 'post request'
      
      if __name__ == '__main__':
          app.run()
      
      
      
      

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多