flask筆記---url、變量規則


1、路由:

route() 裝飾器用於把一個函數綁定到一個 URL,可以動態變化 URL 的某些部分,還可以為一個函數指定多個規則,從而方便用戶訪問與記憶。

例子:

@app.route('/')        #調用一個app的route方法
   def hello_work():      #定義一個處理方法
      return '<h1>hello world</h1>'
   
@app.route(
'/test') #創建第二個應用,並指定訪問路徑 def index(): return 'index page'

全部代碼為:

1  #coding=utf-8                                                                                  
  2 from flask import Flask  #導入flask類
  3 app = Flask(__name__)    #生成該類的一個實例
  4 
  5 @app.route('/')        #調用一個app的route方法
  6 def hello_work():      #定義一個處理方法
  7     return '<h1>hello world</h1>'
  8 
  9 @app.route('/test')    #創建第二個應用,並指定訪問路徑
 10 def index():
 11     return 'index page'
 12 
 13 if __name__ == '__main__': #確保服務器只會在使用python解釋器運行代碼的情況下運行
 14     #app.debug = True     #第一種方式:在應用對象上設置標志
 15     #app.run(host='0.0.0.0')     #用host參數,設定全網段可以訪問 
 16     app.run(debug=True,host='0.0.0.0')   #第二種方式:作為參數傳遞給run方法
View Code

執行程序,狀態為:

# python hello.py 
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
192.168.168.80 - - [09/Apr/2015 15:12:46] "GET / HTTP/1.1" 200 -
192.168.168.80 - - [09/Apr/2015 15:12:51] "GET /test HTTP/1.1" 200 -
View Code

兩次訪問在瀏覽器中的表現為:

             

2、變量規則:

第一個例子:

"第三個應用:標記url的一部分<username>,它可以添加變量,作為關鍵字參數傳遞給函數。"
  @app.route('/user/<username>')  
  def show_user_message(username):
      return 'user %s' % username  

效果截圖:

         

第二個例子:

"第四個應用"
@app.route('/post/<int:post_id>') #轉換器有:intfloat、path(和缺省狀況相同,也接受/ def show_post(post_id): return 'post %d' % post_id

效果截圖:

      

第三個例子:

"第五個應用"
  @app.route('/get/<float:get_nu>')
  def test(get_nu):
      return 'the test outcome: %f' % get_nu  

效果截圖:

至此,全部代碼為:

  8     return '<h1>hello world</h1>'
  9 
 10 @app.route('/test')    #創建第二個應用,並指定訪問路徑
 11 def index():
 12     return 'index page'
 13 
 14 "-------------------------------------二、變量規則例子----------------------------------------"        
 15 "第三個應用:標記url的一部分<username>,它可以添加變量,作為關鍵字參數傳遞給函數。"
 16 @app.route('/user/<username>')  
 17 def show_user_message(username):
 18     return 'user %s' % username  
 19 
 20 " 第四個應用"
 21 @app.route('/post/<int:post_id>')  #轉換器有:intfloat、path(和缺省狀況相同,也接受/
 22 def show_post(post_id):
 23     return 'post %d' % post_id
 24 
 25 "第五個應用"
 26 @app.route('/get/<float:get_nu>')
 27 def test(get_nu):
 28     return 'the test outcome: %f' % get_nu
 29 
 30 
 31 if __name__ == '__main__': #確保服務器只會在使用python解釋器運行代碼的情況下運行
 32     #app.debug = True     #第一種方式:在應用對象上設置標志
 33     #app.run(host='0.0.0.0')     #用host參數,設定全網段可以訪問 
 34     app.run(debug=True,host='0.0.0.0')   #第二種方式:作為參數傳遞給run方法
View Code

3、url重定向

第一個例子:

"第六個應用"
  @app.route('/redirect/')   #行為像文件夾,(若末尾沒有斜杠,Flask會自動重定向加上)
  def redirect():
      return 'The redirect page'   

效果:

   訪問后的效果:

第二個例子:

  "第七個應用"
  @app.route('/redirec')   #行為像文件,若訪問時在末尾加上斜杠,會得到404錯誤
  def redirect():
      return 'The second redirect page'

效果:

   末尾加上斜杠后的效果:

至此,全部代碼為:

1  #coding=utf-8                                                                                             
  2 from flask import Flask  #導入flask類
  3 app = Flask(__name__)    #生成該類的一個實例
  4 
  5 "------------------------------------一、URL例子--------------------------------------------"
  6 @app.route('/')        #調用一個app的route方法
  7 def hello_work():      #定義一個處理方法
  8     return '<h1>hello world</h1>'
  9 
 10 @app.route('/test')    #創建第二個應用,並指定訪問路徑
 11 def index():
 12     return 'index page'
 13 
 14 "-------------------------------------二、變量規則例子----------------------------------------"    
 15 "第三個應用:標記url的一部分<username>,它可以添加變量,作為關鍵字參數傳遞給函數。"
 16 @app.route('/user/<username>')  
 17 def show_user_message(username):
 18     return 'user %s' % username  
 19 
 20 " 第四個應用"
 21 @app.route('/post/<int:post_id>')  #轉換器有:intfloat、path(和缺省狀況相同,也接受/
 22 def show_post(post_id):
 23     return 'post %d' % post_id
 24 
 25 "第五個應用"
 26 @app.route('/get/<float:get_nu>')
 27 def test(get_nu):
 28     return 'the test outcome: %f' % get_nu
 29 
 30 "--------------------------------------三、唯一的URL/重定向行為-----------------------------"
 31 #"第六個應用
 32 #@app.route('/redirect/')   #行為像文件夾,(若末尾沒有斜杠,Flask會自動重定向加上)
 33 #def redirect():
 34  #   return 'The redirect page'
 35 #"
 36 "第七個應用"
 37 @app.route('/redirec')   #行為像文件,若訪問時在末尾加上斜杠,會得到404錯誤
 38 def redirect():
 39     return 'The second redirect page'
 40 
 41 
 42 if __name__ == '__main__': #確保服務器只會在使用python解釋器運行代碼的情況下運行
 43     #app.debug = True     #第一種方式:在應用對象上設置標志
 44     #app.run(host='0.0.0.0')     #用host參數,設定全網段可以訪問 
 45     app.run(debug=True,host='0.0.0.0')   #第二種方式:作為參數傳遞給run方法
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM