'''
2 app.py中的源碼def route(self, rule, **options)
3 @app.route()路由參數使用:
4 1.第一個位置參數為路由分發的請求路徑
5 ①靜態參數路由:/index / /base 等
6 ②動態參數路由:/index/<name> 在路由中使用了<變量名>的路由稱為動態路由,
7 動態路由參數<name>會接收字符串和數字類型,但在制定了int時會優先調用該視圖
8 可以指定int型,如/index/<int:id>,在視圖函數中必須有同名的形參來接收
9
10 2.methods=['GET','PosT']
11 2.1當前視圖函數支持的請求方式(405當前請求方式不被允許),
12 2.2參數為可迭代對象,請求方式不區分大小寫,不設置默認為GET
13
14
15 3.endpoint=''
16 3.1路由映射視圖函數,endpoint不指定默認為視圖函數名(view_func.__name__)
17 3.2項目中存儲視圖函數的view_funcs是以{endpoint:view_func}形式存儲,因此視圖函數不能同名,
18 3.3在使用自定義裝飾器時注意指定唯一的endpoint,以避免在多個視圖函數使用自定義裝飾器時報錯;
19
20
21 4.defaults={key:value}
22 默認參數設置,必須在視圖函數中定義一個形參來接收
23
24 5.redirect_to=''
25 永久重定向(301或者308)
26 應用場景:用戶之前收藏的是視圖函數對應的路由地址,后來頁面不在使用,換了新的路徑,為了避免用戶收藏的不能訪問,因此設置永久重定向
27
28 6.strict_slashes=True/False
29 設置路由路徑匹配是否為嚴格模式,默認不設置為嚴格路由匹配模式
30
31 7.補充小知識:
32 falsk中通過視圖函數名反向解析請求路徑:
33 ①from flask import url_for
34 ②url_for('函數名')==>當前視圖函數對應的路由請求路徑(具體見知識點6)
35 FBV:app.add_url_rule('/',endpoint='',view_func=func) CBV:app.sdd_url_rule('/',endpoint='',view_func=CLASS.as_view(name=''))
36 '''
37 from flask import Flask, render_template, request, session, redirect, url_for
38
39 app = Flask(__name__)
40 app.config['DEBUG'] = True
41 app.secret_key = 'werf23456'
42
43
44 # 1.flaskl路由:
45 # 1.1靜動態路由
46 @app.route('/index')
47 def index():
48 return f'靜態路由請求!'
49
50
51 # 1.2動態參數路由
52 # 1.2.1動態路由匹配變量
53 @app.route('/index/<name>') # http://192.168.16.14:9000/index/yang
54 def index2(name):
55 return f'當前請求的動態參數為:{name}' # 當前請求的動態參數為:yang
56
57
58 # 1.2.2動態路由變量指定匹配
59 @app.route('/index/name=<name>') # http://192.168.16.14:9000/index/name=yang
60 def index3(name):
61 return f'當前請求的動態參數為:{name}' # 當前請求的動態參數name為:yang
62
63
64 # 1.2.3動態路由整數變量匹配
65 @app.route('/index/<int:id>') # http://192.168.16.14:9000/index/1234
66 def index4(id):
67 return f'當前請求的頁碼為:{id}' # 當前請求頁碼為:1234
68
69
70 # 2.methods=[]支持的請求方式參數設置,不設置默認為GET
71 @app.route('/login', methods=['GET', 'PoSt']) # 請求參數設置不區分大小寫,源碼中自動進行了upper
72 def login():
73 if request.method == 'GET':
74 return render_template('login.html')
75 elif request.method == 'POST':
76 username = request.form.get('username')
77 pwd = request.form.get('pwd')
78 if username == 'yang' and pwd == '123456':
79 session['username'] = username
80 return 'login successed 200 ok!'
81 else:
82 return 'login failed!!!'
83
84
85 # 3.endpoint=''路由映射視圖函數
86 '''
87 自定義裝飾器裝飾多個視圖函數時,如果在路由中沒有指定唯一的endpoint,
88 則所有裝飾的視圖函數返回的都是裝飾器中的inner函數,同名因此會報錯
89 AssertionError: View function mapping is overwriting an existing endpoint function: inner
90 '''
91
92
93 def auth(func):
94 def inner(*args, **kwargs):
95 if session.get('username'):
96 return func()
97 else:
98 return '未登錄無權訪問!'
99
100 return inner
101
102
103 @app.route('/data1', endpoint='data1')
104 @auth
105 def data1():
106 return '您已登錄成功,通過路徑/data1來到當前頁面!'
107
108
109 @app.route('/data2', endpoint='data2')
110 @auth
111 def data2():
112 return '您已登錄成功,通過路徑/data2來到當前頁面!'
113
114
115 # 4.defaults={key:value}默認參數
116 @app.route('/key', defaults={'id': 23})
117 def key(id):
118 return f'路由設置的默認參數值為:{id}'
119
120
121 # 5.redirect_to=''永久重定向(301或者308狀態碼)
122 @app.route('/admin', redirect_to='/redirect')
123 def admin():
124 return '原頁面'
125
126
127 @app.route('/redirect')
128 def redi():
129 return '訪問/admin永久重定向的新頁面!'
130
131
132 # 6.strict_slashes=True/False 設置路由路徑匹配是否為嚴格模式,默認不設置為嚴格路由匹配模式
133 #(對於在請求路徑加了/的路徑,在進行路由嚴格模式匹配時,如果在請求時未加/,flask會進行返回308加上/重定向)
134
135 #6.1不設置strict_slashes默認未嚴格模式,在路由后邊沒有/s時,如果訪問加上就會報錯404
136 @app.route('/strict_true', strict_slashes=True)
137 def strict_true():
138 return f'嚴格模式匹配{url_for("strict_true")}'
139
140 #6.2指定strict_slashes=False非嚴格模式時,,在路由后邊加/依然可以正常映射
141 @app.route('/strict_false/', strict_slashes=False)
142 def strict_false():
143 return f'非嚴格模式路由匹配{url_for("strict_false")}'
144
145
146
147 if __name__ == '__main__':
148 app.run('0.0.0.0', 9000)
來源: https://www.cnblogs.com/open-yang/p/11172301.html