- 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.
推薦閱讀
- C#編程入門指南(上下冊)
- 程序員面試算法寶典
- Scratch 3.0少兒編程與邏輯思維訓(xùn)練
- HTML5+CSS3網(wǎng)站設(shè)計教程
- Hands-On GPU:Accelerated Computer Vision with OpenCV and CUDA
- Getting Started with Gulp
- Building RESTful Python Web Services
- C++新經(jīng)典
- Yii Project Blueprints
- JavaScript程序設(shè)計(第2版)
- CRYENGINE Game Development Blueprints
- 現(xiàn)代JavaScript編程:經(jīng)典范例與實踐技巧
- Learning QGIS(Second Edition)
- 亮劍C#項目開發(fā)案例導(dǎo)航
- C#編程魔法書