首页 > 解决方案 > 使用烧瓶将数据从 html 发送到 python 时遇到问题

问题描述

我有一个 HTML 索引页面,它将输入数据发送到一个 python 脚本,该脚本处理数据并将其输出到第二个 HTML 页面中。在本地主机上,该过程可以正常工作,并且可以根据需要显示数据。但是当我尝试在线托管该过程时,我收到一条错误消息,指出找不到 URL。如果有帮助,我正在使用 Heroku。

提前为任何糟糕的术语道歉。我最近才开始学习如何编码。

第一个 HTML

<form action = "https://webaddress/result" method = "POST">
    <h1> Enter info: </h1>
    <input type="text" name="info">

    <input type="submit" value="Submit"/>
</form>

Python:

from flask import Flask, render_template, request
from bs4   import BeautifulSoup
import requests

# https://doi.org/10.2118/21513-MS
app = Flask(__name__)

@app.route('/')
def student():
    return render_template('Trial.html')

@app.route('/result.html',methods = ['POST', 'GET'])
def result():
    return render_template("result.html",result = output)

if __name__ == '__main__':
   app.run(debug = True)

第一个 HTML 中的输入将被发送到 python 部分进行分解和重新排列(省略该部分,以便 python 代码不会太长),然后再输出到 result.html。这适用于使用http://localhost:5000/http://localhost:5000/result的本地主机。

当我在 Heroku 上运行它时,我收到错误消息:

未找到

在此服务器上找不到请求的 URL /result。

更新:问题解决了。

标签: pythonhtml

解决方案


您的表格是指/result(无.html扩展名),但您的路线是/result.html带有扩展名)。尝试.html从您的路线中删除:

@app.route('/result', methods=['POST', 'GET'])
def result():
    return render_template("result.html", result=output)

推荐阅读