我在试着得到我的应用程序路径decorator接受字典键作为参数,而不是单独写出每个函数。你知道吗
from flask import Flask, render_template
app = Flask(__name__)
pages_dict = {"/url": "html_file.html", "/", "index.html"}
for k, v in pages_dict.items():
@app.route(key)
def render():
return render_template(v)
Tags:
在本应使用
,
冒号的地方使用了:
逗号:这很容易纠正为:
@app.route()
修饰符寄存器端点,每个端点必须具有唯一的名称。默认情况下,端点名称取自函数,因此如果要重用函数,则需要显式提供名称:在这里您仍然会遇到闭包问题;
v
中使用的render()
将绑定到循环中的最后一个值。您可能希望将其作为参数传递给render()
:这将
v
绑定为render()
中的局部,而不是将其保留为闭包。详见Local variables in Python nested functions。你知道吗相关问题 更多 >
编程相关推荐