gpt4 book ai didi

python - Flask:[405 错误] 带有分页和 POST 请求

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:33 25 4
gpt4 key购买 nike

我有一个 Flask 应用程序,其路由使用分页并且必须处理 GET 和 POST 请求。我为我的函数定义了两条路由,如here所述使分页正常工作。

当我在第一个页面以外的页面上发布表单时,我会得到一个

"405 - Method Not Allowed" error.

我该如何处理这个问题?我正在使用 peewee 作为我的数据库。

这是 flask 应用程序代码:

@app.route("/tiles/failed", defaults={"page": 1}, methods=["POST", "GET"])
@app.route("/tiles/failed/page/<int:page>", methods=["POST", "GET"])
def tiles_failed(page):
if request.method == "POST":
process_id, tile_name = request.form["tile_name"].split("/")
select_status = request.form["select_status"]
if not select_status == "False":
process = models.Process.select().where(models.Process.id == process_id).get()
tile = models.Tile.select().where(models.Tile.name == tile_name and models.Tile.process == process).get()
tile.message = "Manual state change from '{from_state}' to '{to_state}'".format(from_state=tile.success,
to_state=select_status)
tile.success = select_status
tile.error_count = 0
tile.save()
all_tiles = models.Tile.select().where(models.Tile.success == "False").order_by(models.Tile.name, models.Tile.process)
count = all_tiles.count()
tiles = get_tiles_per_page(all_tiles, page, TILES_PER_PAGE, count)
if not tiles and page != 1:
abort(404)
pagination = Pagination(page, TILES_PER_PAGE, count)
t = app.jinja2_env.get_template("tiles.html")
return t.render(tiles=tiles, pagination=pagination, count=count, request=request)

这里是相关的 HTML 片段:

<form name="select_form" method="post">
<select name="select_status" onchange="if(confirm('are you sure?')){this.form.submit();}else{selectFalse();}">
<option value="False" selected>False</option>
<option value="True">True</option>
<option value="pending">pending</option>
</select>
<input type="hidden" name="tile_name" value="{{ tile.process.id }}/{{ tile.name }}" />
</form>

希望这能解释我的问题。预先感谢您!

最佳答案

您的表单必须有一个 action 属性,该属性指向处理表单的函数。在你的情况下,这个函数是tiles_failed:

<form action="{{ url_for('tiles_failed') }}" name="select_form" method="post">

关于python - Flask:[405 错误] 带有分页和 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31001683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com