- Python Microservices Development
- Tarek Ziadé
- 106字
- 2021-07-02 18:54:21
The url_for function
The last interesting feature of Flask's routing system is the url_for() function. Given any view, it will return its actual URL.
Here's an example with the previous app:
>>> from flask_converter import app
>>> from flask import url_for
>>> with app.test_request_context():
... print(url_for('person', name='Tarek'))
...
/api/person/1
The previous example uses the Read-Eval-Print Loop (REPL), which you can get by running the Python executable directly.
This feature is quite useful in templates when you want to display the URLs of some views depending on the execution context. Instead of hardcoding some links, you can just point the function name to url_for to get it.
推薦閱讀
- .NET之美:.NET關(guān)鍵技術(shù)深入解析
- Learning Docker
- C++面向?qū)ο蟪绦蛟O(shè)計(微課版)
- Visual FoxPro 程序設(shè)計
- 趣學(xué)Python算法100例
- Mastering Swift 2
- 零基礎(chǔ)學(xué)Java(第4版)
- 區(qū)塊鏈國產(chǎn)化實踐指南:基于Fabric 2.0
- 微信小程序開發(fā)實戰(zhàn):設(shè)計·運營·變現(xiàn)(圖解案例版)
- Magento 2 Beginners Guide
- scikit-learn Cookbook(Second Edition)
- Neo4j 3.x入門經(jīng)典
- 從“1”開始3D編程
- Visual C++程序開發(fā)范例寶典
- MATLAB從入門到精通