[英]Taking data from drop-down menu using flask
我是不熟悉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是否足夠的假設是錯誤的?
您只需要告訴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"
將您的代碼修改為:
from flask import Flask
app = Flask(__name__)
@app.route("/submitted", methods=['POST'])
def hello():
return request.form['teamDropdown']
請讓我知道是否有幫助。
因此,您的問題不是關於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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.