我有以下代码
from flask import Flask, render_template, request
app = Flask(__name__)
import logging
@app.route("/")
def hello():
return "Hello csdfwe!"
@app.route('/test/')
def check():
return render_template('template.html')
@app.route('/result/', methods = ['GET', 'POST'])
def result():
print "*****", request.form, "******"
return render_template('template1.html')
@app.route('/result1/', methods = ['GET', 'POST'])
def result1():
return "Pressed OK"
if __name__ == "__main__":
app.run()
template1.html
是
<html>
<body>
<form action = "{{ url_for('result') }}" method = "POST">
<p>Value1 <input type = "text" name = "Value1" /></p>
<p>Value2<input type ="text" name = "Value2" /></p>
<p>Charging Duration <input type ="text" name = "Charging_Duration" /></p>
<p><input type = "submit" value = "submit" name = "submit" /></p>
</form>
</body>
</html>
查询:在html文件中,正如您所看到的,我为Value1和Value2提供了不同的文本框。我想打印文本框后面已经存在的value1的现有值。这意味着模板将显示已存在的值,用户可以设置任何新值。我不知道如何从html函数中获取值。
请指导
答案 0 :(得分:0)
render_template
获取模板中可用的上下文变量。您可以将任意数量的变量传递给模板。
所以,尝试这样的事情:
在你的路线代码中:
@app.route('/result/', methods = ['GET', 'POST'])
def result():
value1 = request.form.get('Value1')
return render_template('template1.html', value1=value1)
template1.html:
<html>
<body>
Value1 is {{value1 or 'not set'}}
<form action = "{{ url_for('result') }}" method = "POST">
<p>Value1 <input type = "text" name = "Value1" /></p>
<p>Value2<input type ="text" name = "Value2" /></p>
<p>Charging Duration <input type ="text" name = "Charging_Duration" /></p>
<p><input type = "submit" value = "submit" name = "submit" /></p>
</form>
</body>
</html>