2024-06-25 07:04:52 发布
网友
函数名(即席测试)是否需要匹配应用程序路径路径?在
from flask import request @app.route('/adhoc_test/') def adhoc_test():
我不太确定内部结构,但是在请求adhoc_测试路由/路径时,到底是什么在执行函数(同名)?在
不,您可以随意命名函数。在
显然,明智地命名您的功能也很重要,这是主要原因之一:
The function is given a name which is also used to generate URLs for that particular function, and returns the message we want to display in the user’s browser.
下面是一个示例,说明为什么使用相关函数名非常方便(使用示例为url_):
from flask import Flask, url_for app = Flask(__name__) @app.route('/') def index(): return 'index' @app.route('/login') def login(): return 'login' @app.route('/user/<username>') def profile(username): return '{}\'s profile'.format(username) with app.test_request_context(): print(url_for('index')) print(url_for('login')) print(url_for('login', next='/')) print(url_for('profile', username='John Doe'))
您可以在Flask's Documentation中阅读此信息的其他详细信息。在
不,函数的名称无关紧要(即它不必与路由匹配),只要您没有多个同名的函数(那么在运行服务器时会出现实际错误)
AssertionError: View function mapping is overwriting an existing endpoint function
but what exactly is executing the function
它比这复杂一点,但归根结底,flask保存了一个字典,作为“端点”(函数名)和函数对象(这就是函数名必须唯一的原因)之间的映射:
它还保留一个url_map来将路由映射到函数:
url_map
Map([<Rule '/route_a' (OPTIONS, GET, HEAD) -> func_a>, <Rule '/route_b' (OPTIONS, GET, HEAD) -> func_b>, <Rule '/static/<filename>' (OPTIONS, GET, HEAD) -> static>]) {}
不,您可以随意命名函数。在
显然,明智地命名您的功能也很重要,这是主要原因之一:
下面是一个示例,说明为什么使用相关函数名非常方便(使用示例为url_):
您可以在Flask's Documentation中阅读此信息的其他详细信息。在
不,函数的名称无关紧要(即它不必与路由匹配),只要您没有多个同名的函数(那么在运行服务器时会出现实际错误)
它比这复杂一点,但归根结底,flask保存了一个字典,作为“端点”(函数名)和函数对象(这就是函数名必须唯一的原因)之间的映射:
^{pr2}$它还保留一个
url_map
来将路由映射到函数:相关问题 更多 >
编程相关推荐