我正在尝试从列表中动态生成Flask的路由。我想要动态生成视图函数和端点,并使用add_url_rule
添加它们。
这是我尝试做的事情,但我遇到了“mapping overwrite”错误:
routes = [
dict(route="/", func="index", page="index"),
dict(route="/about", func="about", page="about")
]
for route in routes:
app.add_url_rule(
route["route"], #I believe this is the actual url
route["page"], # this is the name used for url_for (from the docs)
route["func"]
)
app.view_functions[route["func"]] = return render_template("index.html")
Jinja2
模板如何与我最近的追问有关。 - A. Vidorapp.route('/<page>')
。装饰的视图函数将接受一个名为page
的参数,并根据该参数委派其他操作。我会更新我的答案,包括这个解决方案。 - A. Vidor