HTMLFlask按钮无法正确重定向到主URL

2024-05-13 18:35:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个HTML按钮。一个用于启动任务,另一个用于停止任务:

开始时间:

<form action="localhost:5000/data" method="POST">
  <input type="submit" name="Start Data Collection" value="Start Data Collection"/>
</form>

停止:

^{pr2}$

我正在使用flask来构建我的web服务器。处理启动按钮的烧瓶代码是:

@app.route('/data', methods=['POST'])
def recvdata():
    message = {"message":"start_socket"}
    messageQueue.put(message)
    return redirect("/")

停下来:

@app.route('/stop', methods=['POST'])
def stop_task():
    message = {'message':'stop'}
    messageQueue.put(message)
    return redirect("/")

这两个按钮都是为了在处理后重定向回主页。启动按钮没有问题。停止按钮重定向到localhost:5000/localhost:5000/stop,我似乎不明白为什么。我做错什么了?在


Tags: formapplocalhostmessagedatadefpost按钮
1条回答
网友
1楼 · 发布于 2024-05-13 18:35:26

无论何时确定链接,如果该链接不是以协议或//开头的,它将被附加到当前路径。在

因此,与其action="localhost:5000/stop",不如将其设置为action="//localhost:5000/stop",强制它使用您现在使用的相同协议(http或https)。在

action="localhost:5000/data"也应该这样做,变成action="//localhost:5000/data"

相关问题 更多 >