我正在使用一個簡單的 csv 檔案,它有 1 行,沒有標題,包含兩個欄位(兩個日期)。下面的 Python 腳本和 HTML 到目前為止有效,但在用戶(我)可以更改日期之前,我無法使用 csv 的單元格內容填充日期欄位。我已經嘗試了好幾天了。
HTML (form.html):
<form action="{{ url_for("getdates")}}" method="post">
<label for="first">1st Ferment Date:</label>
<input type="date" id="first" name="first" placeholder="first" value=firsttrial>
{{ firsttrial }}
<br>
<br>
<label for="bottled">Bottling Date:</label>
<input type="date" id="bottled" name="bottled" placeholder="bottled">
<br>
<br>
<button type="submit">click here to Reset</button>
Python/Flask code (getdates.py)"
from flask import Flask, request, render_template
import os
import csv
app = Flask(__name__)
dir = os.path.dirname(__file__)
with open(dir 'Data/variable.csv') as f:
data = list(csv.reader(f))
print('after list(csv.reader)' data[0][0])
firsttrial = data[0][0]
#try:
# last_name = data[0][1]
#except:
# print("not working")
#else:
# print("last" last_name)
f.close()
@app.route('/', methods =["GET", "POST"])
def getdates():
templateData = {
'firsttrial' : firsttrial
}
if request.method == "POST":
# getting input with name = fname in HTML form
first = request.form.get("first")
if first:
# getting input with name = lname in HTML form
bottled = request.form.get("bottled")
input_dictionary = bottled
dir = os.path.dirname(__file__)
tempsfile = dir "Data/variable.csv"
import csv
RESULT = [first, bottled]
resultFile = open(tempsfile,'w')
wr = csv.writer(resultFile, dialect='excel')
wr.writerow(RESULT)
resultFile.close()
else:
return "you did not enter the word 'reset', so counter will continue as is"
return render_template("form.html",**templateData)
if __name__=='__main__':
app.run()
任何幫助,將不勝感激!
uj5u.com熱心網友回復:
這:
<input type="date" id="first" name="first" placeholder="first" value=firsttrial>
{{ firsttrial }}
應該是這樣的:
<input type="date" id="first" name="first" placeholder="first" value="{{firsttrial}}">
它value是指定放置在欄位中的內容的屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/391081.html
上一篇:Flask-表單資料沒有?
