本節內容
1、回顧:Python你已經知道了甚么?
2、構建一個Web應用的流程
3、安裝Flask
4、Flask的工作原理
5、路由規則、url構建
6、http方法
7、靜態文件
8、渲染模板
9、在html頁面中加載靜態資源
1、回顧:Python你已經知道了甚么?


2、構建一個Web應用的流程


3、安裝Flask
在使用pycharm中創建Flask:
第一種方法:
1.可以先建Pure Python

然后在項目python環境中添加flask環境:


在project:201909 中install Package <==>在命令行創建一個envy 201909環境,然后 pip install flask
第二種方法:
可以直接在pycharm中創建flask項目

4、Flask的工作原理
1 # -*- coding:utf-8 -*- 2 # Author:Zhichao 3 4 from flask import Flask 5 6 app = Flask(__name__) 7 8 9 @app.route('/') 10 def hello() -> str: 11 """向web打招呼""" 12 return 'Hello world from Flask' 13 14 app.run(debug=True)
Flask基本原理
"""The flask object implements a WSGI application and acts as the central
object. It is passed the name of the module or package of the
application. Once it is created it will act as a central registry for
the view functions, the URL rules, template configuration and much more.
The name of the package is used to resolve resources from inside the
package or the folder the module is contained in depending on if the
package parameter resolves to an actual python package (a folder with
an :file:`__init__.py` file inside) or a standard module (just a ``.py`` file).
For more information about resource loading, see :func:`open_resource`."""

app.run()參數介紹:
def run(self, host=None, port=None, debug=None, load_dotenv=True, **options):
5、路由規則、url構建
參考:https://flask.palletsprojects.com/
http://www.pythondoc.com/flask/index.html
5.1 路由
現代 web 應用都使用有意義的 URL ,這樣有助於用戶記憶,網頁會更得到用戶的青睞, 提高回頭率。
使用 route() 裝飾器來把函數綁定到 URL:
1 @app.route('/') 2 def index(): 3 return 'Index Page' 4 5 @app.route('/hello') 6 def hello(): 7 return 'Hello, World'
5.2 變量規則
通過把 URL 的一部分標記為 <variable_name> 就可以在 URL 中添加變量。標記的
部分會作為關鍵字參數傳遞給函數。通過使用 <converter:variable_name> ,可以
選擇性的加上一個轉換器,為變量指定規則。請看下面的例子:
1 # -*- coding:utf-8 -*- 2 # Author:Zhichao 3 4 from flask import Flask, url_for 5 6 app = Flask(__name__) 7 8 @app.route('/') 9 def hello() -> str: 10 """向web打招呼""" 11 return 'Hello world from Flask' 12 13 @app.route('/about') 14 def about(): 15 """show the user about for that user""" 16 return 'The about page' 17 18 @app.route('/projects/') 19 def projects(): 20 """show the user projects for that user""" 21 return 'The project page' 22 23 @app.route('/user/<username>') 24 def show_user_profile(username): 25 """show the user profile for that user""" 26 return 'User %s' % username 27 28 @app.route('/post/<string:post_username>/<int:post_id>') 29 def show_post(post_username, post_id): 30 """show the post with the given id, the id is an integer""" 31 return 'Username %s id %d' % (post_username, post_id) 32 33 @app.route('/path/<path:subpath>') 34 def show_subpath(subpath): 35 """show the subpath after /path/""" 36 return 'Subpath %s' % subpath 37 38 app.run(debug=True)

5.3 唯一的 URL / 重定向行為
以下兩條規則的不同之處在於是否使用尾部的斜杠 '/':
1 @app.route('/projects/') 2 def projects(): 3 return 'The project page' 4 5 @app.route('/about') 6 def about(): 7 return 'The about page'
projects的URL是中規中矩的,尾部有一個斜杠,看起來就如同一個文件夾。訪問一個沒有斜杠結尾的URL時Flask會自動進行重定向,幫你在尾部加上一個斜杠。
about的URL沒有尾部斜杠,因此其行為表現與一個文件類似。如果訪問這個URL時添加了尾部斜杠就會得到一個 404 錯誤。這樣可以保持URL唯一,並幫助搜索引擎避免重復索引同一頁面。
5.4 URL 構建
為什么不在把 URL 寫死在模板中,而要使用反轉函數 url_for() 動態構建?
-
反轉通常比硬編碼 URL 的描述性更好。
-
你可以只在一個地方改變 URL ,而不用到處亂找。
-
URL 創建會為你處理特殊字符的轉義和 Unicode 數據,比較直觀。
-
生產的路徑總是絕對路徑,可以避免相對路徑產生副作用。
-
如果你的應用是放在 URL 根路徑之外的地方(如在
/myapplication中,不在/中),url_for()會為你妥善處理。
1 from flask import Flask, escape, url_for 2 3 app = Flask(__name__) 4 5 @app.route('/') 6 def index(): 7 return 'index' 8 9 @app.route('/login') 10 def login(): 11 return 'login' 12 13 @app.route('/user/<username>') 14 def profile(username): 15 return '{}\'s profile'.format(escape(username)) 16 17 with app.test_request_context(): 18 print(url_for('index')) 19 print(url_for('login')) 20 print(url_for('login', next='/')) 21 print(url_for('profile', username='John Doe'))
6.HTTP方法
根據 HTTP 標准,HTTP 請求可以使用多種請求方法。

比較 GET 與 POST

