1 from flask import Flask;------->引入Flask插件,pip install Flask; 2 3 app=Flask(__name__) #變量app是Flask的一個實例並且必須傳入一個參數,__name__對應的值是__main,即當前的py文件的文件名作為Flask的程序名稱,這個也可以自定義,比如,取,'MY_ZHH_APP'
#__name__是固定寫法,主要是方便flask框架去尋找資源 ,也方便flask插件出現錯誤時,去定位問題
4 5 @app.route('/') #相當於一個裝飾器,視圖映射,路由系統生成 視圖對應url,這邊沒有指定method .默認使用get 6 def first_flask(): #視圖函數 7 return 'Hello World' #response,最終給瀏覽器返回的內容 8 9 10 if __name__ == '__main__': 11 app.run(debug=True) #啟動這個應用服務器,並開啟debug,才能定位問題
運行結果:
終端中顯示:
運行過程及原理:
*當客戶端訪問/
時,將響應hello_world()
函數返回的內容。
*同理如果route('/index'),那么就是客戶端訪問'/index'時,將相應hello_world()函數返回的內容
*app.run()當時運行,若是服務端出問題,那么是不會在客戶端展示的,但是在調試過程中,顯示這個錯誤是有必要的,因此要加上:app.run(debug=True);
*默認情況下,Flask綁定的IP和端口分別為:127.0.0.1、5000,但實際我們可以自定義綁定,如0.0.0.0代表的是電腦的所有IP,端口80是HTTP的默認端口------------木有成功啊,為什么
1 app.run(host='0.0.0.0',port=8089,debug=True);
注意,這不是返回Hello World!
這么簡單,Hello World!
只是HTTP響應報文的實體部分,狀態碼等信息既可以由Flask自動處理,也可以通過編程來制定。
1、URL的參數
*以鍵值對存儲,比如要查看當前URL的參數,列出URL的所有參數
1 @app.route('/index') 2 def first_flask(): #視圖函數 3 return request.args.__str__() #返回'/index'URL的所有參數
運行結果(參數是空的):
如果在地址后加上問號和參數,即可得到參數的鍵值對
2、瀏覽器傳給Flask服務器的數據長什么樣子(即每次在瀏覽器刷新一次,Flask都會接受瀏覽器給的數據)
@app.route('/index') def first_flask(): print(request.path);------>運行結果:【/index】 print(request.full_path);----------運行結果:【index?】 return request.args.__str__() #response
3、獲取URL的某個指定的參數
1 @app.route('/index') 2 def first_flask(): 5 return request.args.get('info')----->最終reponse返回,url中info參數的值
運行結果:
*注意若URL后的參數中沒有info值,那么最終執行會報錯(TypeError),正常python會返回None,但是Flask不允許返回None
*解決辦法1:在打印出來前判斷是否存在,或是否為None
*解決辦法2:r=request.args.get('info','我是代替的默認值');,判斷info是否存在,如果不存在,那么,打印第二個默認值
V V V V
1 r=request.args.get('info'); 2 if r==None: 3 return '不存在'; 4 else: 5 return r;
3、獲取URL的多個參數(getlist)
@app.route('/index') def first_flask(): r=request.args.getlist('info'); if r==None: return '不存在'; else: return str(r);------->注意最后返回的是列表類型,必現將類型轉換成str,否則會報TypeError錯誤
4、url_for(反轉url:主要處理頁面重定向,URL的獲取),要導入url_for
1 @app.route('/index') 2 def index(): 3 print(url_for('my_layout',username='aaa'));------>url_for的第一個參數是視圖方法名,比如my_layout,hello_world, 4 print(url_for('hello_world',username='bbb'));----->第二個參數是參數,如有沒有可以不寫,如有有參數,必須要寫(有幾個寫幾個),<參數名>='給值' 5 return 'index'; 6 7 #layOut.html 8 9 @app.route('/hello/<username>') 10 def hello_world(username): 11 return render_template('hello.html',username=username); 12 13 14 @app.route('/layout/<username>') 15 def my_layout(username): 16 return render_template('jicheng.html',username=username);
最終看運行結果,可以看到打印出該視圖方法的URL地址
4、url_for的頁面重定向小例子
@app.route('/hello/<username>') def hello_world(username): return render_template('hello.html',username=username); @app.route('/layout/<username>') def my_layout(username): return render_template('jicheng.html',username=username); @app.route('/login/<is_login>') def login(is_login): if is_login==1:------------------------>判斷is_login是否等於1,如果是,則跳轉到【my_layout】視圖所對應的頁面 return redirect(url_for('my_layout',username=is_login)); else: return redirect(url_for('hello_world',username='你還沒登陸呢,請先登陸'))----------如果不是,則跳轉到【hello_world】視圖所對應的頁面 if __name__ == '__main__': app.run(debug=True);