- 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.
推薦閱讀
- 數據科學實戰手冊(R+Python)
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- C語言程序設計(第3版)
- Learn Type:Driven Development
- Vue.js 2 and Bootstrap 4 Web Development
- 深入理解Django:框架內幕與實現原理
- vSphere High Performance Cookbook
- 實用防銹油配方與制備200例
- Mastering Python High Performance
- Hands-On RESTful Web Services with Go
- JavaScript by Example
- Visual Basic程序設計
- App Inventor創意趣味編程進階
- C指針原理揭秘:基于底層實現機制
- Clojure Polymorphism