使用烧瓶从下拉菜单中获取数据

时间:2018-09-21 12:32:00

标签: python html flask

我是不熟悉烧瓶的人,对如何解决这个问题真的完全迷失了。我研究了其他SO问题,但是无论如何我似乎都无法解决这个问题。

我有这样的表格:

<form class="teamSelection" method="POST" action="/submitted">  
   <select class="teamForm" id="teamDropdownSelector" type="text" name="teamDropdown" placeholder="Select A Team">
        <option disabled selected>Select a game</option>
        <option id="WatfordVSManchester Utd" value="">Watford VS Manchester Utd</option>
   </select>
   <input class="btn" type="submit" value="submit">
</form>

和我的烧瓶一样:

from flask import Flask
app = Flask(__name__)

@app.route("/submitted")
def hello():
    return "hello world"

目标是获取选定/提交的下拉菜单项的内容,并将其传递给flask文件,然后在其中使用团队名称来获取有关比赛的信息。但是此刻,我什至似乎无法使表格的POST正常工作,并且完全不知所措。我赞赏这是一个模糊且开放的问题,但我真的不知道该如何解决。

我应该使用jquery来检测下拉列表何时更改,并使用AJAX发送POST以某种方式调用脚本并将值传递给它吗?

任何帮助将不胜感激。


编辑
我以为我把它放在了原始帖子中,但是一定忘了。
我目前正在运行一个Apache localhost服务器,并且正在通过pycharm使用flask。我目前所做的只是将flask软件包安装在pycharm中,并且没有像在某些教程中看到的那样在命令行运行时进行任何设置。我以为没有必要执行此步骤,因为我已经启动了服务器并可以使用apache运行它?
当涉及到这样的后端东西时,我真的不知道,所以如果这是一个愚蠢的假设,我深表歉意。

我将烧瓶更改为:

from flask import Flask
app = Flask(__name__)

@app.route("/submitted", methods=['POST'])
def hello():
    with open("newTest.csv", mode="w+") as file:
        fileWriter = csv.writer(file)
        fileWriter.writerow(['Time', 'HomeTeam', 'AwayTeam'])
    file.close()

原因是,正如我所看到的,实际上是否正在调用此脚本,否则将创建一个名为newTest的新csv文件。在运行网页并没有提交新的csv文件后,因此该脚本未在运行,这可能是由于我未正确配置flask?/ apache够用的假设是错误的吗?

3 个答案:

答案 0 :(得分:2)

您只需告诉flask方法接受POST请求并从请求中读取参数

示例:

from flask import Flask, request
app = Flask(__name__)

@app.route("/submitted", methods=['POST'])
def hello():
   myvariable = request.form.get("teamDropdown")
   ... your code ...
   return "hello world"

答案 1 :(得分:0)

将代码修改为:

from flask import Flask
app = Flask(__name__)

@app.route("/submitted", methods=['POST'])
def hello():
    return request.form['teamDropdown']

请告诉我是否有帮助。

答案 2 :(得分:0)

因此,您的问题不是关于flask,而是关于fopen-您必须添加完整的文件路径,包括目录路径script_dir = path.dirname(path.abspath(__file__))

Flask脚本(已修改为在我的本地项目副本中启动):

from flask import Flask, render_template, request
import csv
from os import path
app = Flask(__name__)

script_dir = path.dirname(path.abspath(__file__))

@app.route ("/")
def index():
    return render_template("index.html") 

@app.route("/submitted", methods=["GET", "POST"])
def hello():
    if request.method == "GET":
        return render_template("index.html") 
    filefullpath = script_dir + '//newTest.csv'
    with open(filefullpath, mode="w+") as file:
        fileWriter = csv.writer(file)
        fileWriter.writerow(['Time', 'HomeTeam', 'AwayTeam'])
    file.close()
    return "hello world"

index.html(在“ / templates”文件夹中)

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    Test
        <br>
    <form class="teamSelection" method="POST" action="/submitted">  
       <select class="teamForm" id="teamDropdownSelector" type="text" name="teamDropdown" placeholder="Select A Team">
            <option disabled selected>Select a game</option>
            <option id="WatfordVSManchester Utd" value="">Watford VS Manchester Utd</option>
       </select>
       <input class="btn" type="submit" value="submit">
    </form>
</body>
</html>
相关问题