我有一个Flask应用程序,其路由使用分页并且必须处理GET和POST请求。 我为我的函数定义了两个路由,如here所述 让分页工作。
当我在第一页以外的页面上发布表格时,我会得到一个
“405 - 方法不允许”错误。
我该如何处理?
我正在使用peewee
作为我的数据库。
这是烧瓶应用代码:
@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>
希望这能解释我的问题。 提前谢谢!
答案 0 :(得分:1)
您的表单必须具有action
属性,该属性指向处理表单的函数。在您的情况下,此函数为tiles_failed
:
<form action="{{ url_for('tiles_failed') }}" name="select_form" method="post">