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

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.

主站蜘蛛池模板: 海宁市| 扎鲁特旗| 日土县| 张掖市| 花莲县| 白朗县| 九龙县| 台江县| 东源县| 邻水| 中山市| 封丘县| 长海县| 曲阳县| 佛山市| 盖州市| 赤壁市| 塘沽区| 台前县| 龙川县| 白水县| 晋中市| 桃源县| 石林| 泸溪县| 手游| 洪泽县| 郓城县| 蕲春县| 紫金县| 周口市| 成都市| 察隅县| 九江县| 临朐县| 武城县| 六安市| 福建省| 尉氏县| 姜堰市| 宜兰县|