python框架flask入門之路由及簡單實(shí)現(xiàn)方法
路由
簡單來說,路由就是一個(gè)url到函數(shù)的映射,通過路由規(guī)則,可以使得url被指定的函數(shù)進(jìn)行處理解析。
我們都知道現(xiàn)在的web系統(tǒng)的URL都是可以自定義的,也就是我們可以指定url和具體的業(yè)務(wù)控制器相關(guān)聯(lián),而這些就是通過路由來實(shí)現(xiàn)的。
flask中集成了路由處理模塊,我們只需要簡單地使用route裝飾器就可以實(shí)現(xiàn)路由匹配。
@app.route(’/’)def index(): return ’Index Page’@app.route(’/hello’)def hello(): return ’Hello, World’
上面的例子中,我們訪問瀏覽器的時(shí)候,比如輸入http://127.0.0.1/ 就會(huì)返回’index page’,當(dāng)輸入http://127.0.0.1/hello 就會(huì)返回‘Hello World’,這就是路由的基本使用。
@app.route(’/user/<username>’)def show_user(username): return username@app.route(’/post/<int:post_id>’)def show_post(post_id): return ’Post %d’ % post_id@app.route(’/path/<path:subpath>’)def show_subpath(subpath): # show the subpath after /path/ return ’Subpath %s’ % escape(subpath)
從上面的例子我們可以看出,flask的路由還可以進(jìn)行參數(shù)匹配,比如我們可以通過<>來對參數(shù)進(jìn)行獲取,可以獲取到文章的id,獲取到用戶名等參數(shù)信息,也就是說,我們可以通過路由匹配來對指定的url進(jìn)行處理。
關(guān)于url中斜線(/)的處理當(dāng)我們在路由中定義了斜線,那么當(dāng)我們訪問沒有斜線的url的時(shí)候,它會(huì)自動(dòng)添加斜線當(dāng)我們在路由中沒有定義斜線的時(shí)候,那么我們訪問有斜線的時(shí)候,會(huì)提示404
@app.route(’/test/’)#當(dāng)我們訪問http://127.0.0.1/test的時(shí)候,會(huì)重定向到http://127.0.0.1/test/def test(): return ’test’@app.route(’slashes’)#當(dāng)我們訪問http://127.0.0.1/slashes/的時(shí)候,會(huì)提示404,無法匹配到路由def slashes(): return ’slashes’
下面看下Python Flask框架路由簡單實(shí)現(xiàn)
也許你聽說過Flask框架。也許你也使用過,也使用的非常好。但是當(dāng)你在瀏覽器上輸入一串路由地址,跳轉(zhuǎn)至你所寫的頁面,在Flask中是怎樣實(shí)現(xiàn)的,你是否感到好奇? 如果你不清楚,那就往下看看吧 ~
Flask demo代碼
from flask import Flaskapp = Flask(__name__)@app.route(’/’)def hello_world(): return ’<a href='http://www.intensediesel.com/home'>Hello World!</a>’@app.route('/home')def home(): return '這個(gè)是主頁'@app.route('/login')def login(): return '這個(gè)是登錄'if __name__ == ’__main__’: app.run(debug=True)
當(dāng)你在瀏覽器地址上輸入路徑,進(jìn)入不同視圖函數(shù)。
1.輸入路徑為:'/'
2.輸入路徑為:'/home'
3.輸入路徑為:'/login'
是不是很神奇~ 接下來讓我們看看他是如何實(shí)現(xiàn)的吧!
簡單實(shí)現(xiàn)(廢話不多說,直接上碼?。?/p>
''' Flask 路由簡單實(shí)現(xiàn) map: {'/home':'home', '/login':'login', ...}'''map = dict()def route(path): def decorator(f): print(path, f.__name__, '<-----------start---------->') map[path] = f.__name__ return f return decoratorclass TestFlaskRoute: def __init__(self, path): self.path = path def find_url(self): try: getattr(TestFlaskRoute, map.get(self.path)).__call__(self) except: print('033[031m 沒有這個(gè)路徑! 033[0m ') @route('/login') def login(self, *args, **kwargs): print('033[036m 這個(gè)是登錄! 033[0m') pass @route('/home') def home(self, *args, **kwargs): print('033[036m 這個(gè)是主頁! 033[0m') pass @route('/') def index(self, *args, **kwargs): print('033[036m Hello World! 033[0m') pass @route('/call') def phone(self, *args, **kwargs): print('033[036m 這個(gè)是', self.path, '! 033[0m') passif __name__ == ’__main__’: while True: url = input('請輸入U(xiǎn)RL路徑:') app = TestFlaskRoute(url) app.find_url()
在這里我們用模擬瀏覽器輸入方式來完成,當(dāng)我們輸入'/',它會(huì)去找對應(yīng)路徑下index() 執(zhí)行 ; 輸入'/home',它會(huì)去找對應(yīng)路徑下home() 執(zhí)行;輸入'/login',它會(huì)去找對應(yīng)路徑下 login() 執(zhí)行,怎樣是不是很神奇,是不是很像Flask。其實(shí)Flask就是這樣的思路實(shí)現(xiàn)的。
效果:
解析實(shí)現(xiàn)(3步)
a.當(dāng)程序啟動(dòng)時(shí),先執(zhí)行了route()這個(gè)裝飾器,拿到了所有路徑、對應(yīng)函數(shù)并將它們存在map這個(gè)字典中,以路徑為key,對應(yīng)函數(shù)名為value。(注:在Flask是以Route類來存這些信息的哦,所以還是有些區(qū)別的~)
b.當(dāng)你輸入路徑或者在瀏覽器上輸入路徑,會(huì)以key的形式到map字典中匹配,取出value(函數(shù)名)。
c.得到value通過getattr()得到函數(shù)地址,然后用內(nèi)置__call__()方法,執(zhí)行這個(gè)函數(shù)
總結(jié)
到此這篇關(guān)于python框架flask入門之路由及簡單實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)python框架flask 路由內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. JavaWeb Servlet中url-pattern的使用2. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長日期的方法3. asp知識(shí)整理筆記4(問答模式)4. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?5. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁6. ASP實(shí)現(xiàn)加法驗(yàn)證碼7. XML解析錯(cuò)誤:未組織好 的解決辦法8. 小技巧處理div內(nèi)容溢出9. js的一些潛在規(guī)則使用分析10. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)
