python - Flask路由查询参数

标签 python rest flask routes werkzeug

假设我用 Flask 设置了两条路由:

app.add_url_rule('/example', view_func = example.Index.as_view('example'))
app.add_url_rule('/example/<string:example_key>', view_func = example.Show.as_view('example'), methods=['GET'])

这会将/example 路由到 example.Index 并将/example/1 路由到 example.Show。

当我转到/example?parameter=miau 时,它会路由到 example.Show 而不是 example.Index。

为什么会发生这种情况以及我应该如何解决这个问题?

最佳答案

发现,注意“example.Index.as_view('example')”,它应该是:

app.add_url_rule('/example', view_func = example.Index.as_view('example_index'))
app.add_url_rule('/example/<string:example_key>', view_func = example.Show.as_view('example_show'), methods=['GET'])

关于python - Flask路由查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11700698/

相关文章:

javascript - 如何通过 REST 使用 Kinvey 执行 'Hello World'?

api - 在有争议的情况下选择合适的 HTTP 状态码还是引入子码?

Flask-wtform 大小写敏感问题

python - 并行使用两个迭代器

python - Ipython 笔记本输出静音

python - 在 cherrypy 中成功快速启动后启动后台进程

python - 如何在flask-WTF和wtforms的字符串字段中进行新行/多行输入

python - 使用 Python + Flask-SqlAlchemy 的默认查询范围

python - 分别为多个文件保存每个文件中的多个图形

python - 微调时使用 HuggingFace 库进行标记时出现问题