官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 河曲县| 安远县| 周宁县| 青海省| 临清市| 桂林市| 务川| 嘉禾县| 汝阳县| 忻州市| 嘉黎县| 应用必备| 金阳县| 宿松县| 宁远县| 密山市| 兴安县| 咸丰县| 巴彦淖尔市| 大英县| 丹阳市| 潼关县| 石首市| 韶山市| 尼勒克县| 丹巴县| 龙泉市| 怀集县| 武夷山市| 维西| 宜春市| 淄博市| 涟水县| 柯坪县| 勃利县| 靖西县| 仁寿县| 醴陵市| 郎溪县| 梨树县| 九龙县|