Pre-Populating HTML 表单,日期,带有 Flask 变量内容
Pre-Populating HTML form, date, with Flask variable content
我正在处理一个简单的 csv 文件,它有 1 行,没有 header,包含两个字段(两个日期)。下面的 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()
如有任何帮助,我们将不胜感激!
这个:
<input type="date" id="first" name="first" placeholder="first" value=firsttrial>
{{ firsttrial }}
应该是这样的:
<input type="date" id="first" name="first" placeholder="first" value="{{firsttrial}}">
value
属性指定要放置在字段中的内容。
我正在处理一个简单的 csv 文件,它有 1 行,没有 header,包含两个字段(两个日期)。下面的 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()
如有任何帮助,我们将不胜感激!
这个:
<input type="date" id="first" name="first" placeholder="first" value=firsttrial>
{{ firsttrial }}
应该是这样的:
<input type="date" id="first" name="first" placeholder="first" value="{{firsttrial}}">
value
属性指定要放置在字段中的内容。