- 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.
推薦閱讀
- Web應用系統開發實踐(C#)
- AngularJS入門與進階
- 體驗設計原理:行為、情感和細節
- Python數據分析(第2版)
- The HTML and CSS Workshop
- C#程序設計基礎:教程、實驗、習題
- Learning Hunk
- Microsoft Dynamics AX 2012 R3 Financial Management
- Geospatial Development By Example with Python
- C語言程序設計與應用(第2版)
- STM8實戰
- Android嵌入式系統程序開發(基于Cortex-A8)
- 虛擬現實:引領未來的人機交互革命
- C/C++程序設計教程
- Roslyn Cookbook