将烧瓶中的HTML输入并返回数据

时间:2020-05-15 10:06:07

标签: python html function flask input

我想从特定URL的搜索字段中获取输入,并在Flask上通过它运行一个函数。但是,即使我只运行@ app.route(“ /”),我也会不断得到405方法的不允许,即使我希望返回数据。

@app.route('/searchcountry', methods = ["GET"])
def single():
    return render_template("singlecountry.html")

@app.route('/searchcountry', methods = ["POST"])
def singlecountry():
    countrydata = request.form["textfield1"]
    alldata = allfacts(countrydata)
    return render_template("singlecountry.html", alldata = alldata, countrydata = countrydata) 

在这里,我尝试使用所需的数据更新/ searchcountry。

<!DOCTYPE html> 
<html>
    <head></head>
    <body>
        <nav>
            <a href="/home">homepage</a>
            <a href="/singlecountry">All data on one country</a>
            <a href="/comparedcountries">Compare two countries</a>
        </nav>
        <h1>Data about {{countrydata}}</h1>
        <h4>Data is collected from the WHO:<br><a href="https://apps.who.int/gho/portal/uhc-cabinet-wrapper-v2.jsp?id=1010501">Universal Health Coverage</a><br> 
            As well as the Happy Planet Index:<br><a href="http://happyplanetindex.org/">The Happy Planet Index</a></h4>

        <form id="search" name="search" action="." method="POST">
             <div id="searchfield">
                All data on a country:<br><input type="text"  name="textfield1">
           </div>
           <input type="submit">
        </form>

        <h3>{{countrydata}}</h3>
        {% for data in alldata %}
         <tr>
        <td>{{data}}</td>
             <br>
        </tr>
         {% endfor %}

    </body> 

</html>

我的html代码。我知道该功能有效。

0 个答案:

没有答案