我的app.py看起来像这样:
from flask import Flask,render_template,url_for,request
from flask_bootstrap import Bootstrap
from wtforms import SelectField
from flask_wtf import FlaskForm
app = Flask(__name__)
Bootstrap(app)
def Form(FlaskForm):
panel_brand = SelectField('Panel Brand',choices=[('tr','trina'),('lo','longi'), ('ph','phono'), ('leap','leapton')])
mounting = SelectField('Mounting',choices=[('tr','trina'),('lo','longi'), ('ph','phono'), ('leap','leapton')])
inverter = SelectField('inverter',choices=[('Fronius10','Fronius 10'),('Fronius5','Fronius 5'), ('huawei6','huawei 6'), ('huawei8','huawei 8') , ('SolarEdge125','SolarEdge 12,5')])
order_fulfillment = SelectField('orderfulfillment',choices=[('all3','Panels + inverter + mounting'),('Panels','Panels'),('panelsinv','Panels + Inverter'),('installer','By Installer')])
@app.route('/', methods=['GET','POST'])
def index():
form = Form(FlaskForm)
return render_template('index.html', form=form)
if __name__ == '__main__':
app.run(debug=True)
而且我的index.html看起来像这样
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form method="POST">
{{ form.csrf_token }}
{{ form.panel_brand }}
{{ form.mounting }}
{{ form.inverter }}
{{ form.order_fulfillment }}
<input type="submit">
</form>
</body>
</html>
当我运行app.py文件时,它仅显示提交按钮,而不显示预期的下拉列表panel_brand
,mounting
,inverter
,order_fulfillment
,其中包含后续值。我想我在index()
函数中缺少某些呼叫ID?我不确定如何用正确的语法来做到这一点。有人可以帮我吗?
您的代码具有需要一个类的功能。
更改
def Form(FlaskForm):
...
form = Form(FlaskForm)
to
class Form(FlaskForm)
...
form = Form(request.form)
和
似乎您的form
声明正在继承另一个对象。
尝试:
def index():
form = Form(request.form)