如何在 Flask 应用程序模型中添加带有值(预定义)的下拉列表?
How to add a drop down with values(pre defined) in flask app models?
我是 flask 的新手,我想将下拉列表添加到具有预定义值(不是从数据库中获取)的表单中。我创建了一个模型如下。
class DeliveryDetails(Model):
_tablename_ = 'deliverydetails'
id = Column(Integer, primary_key=True)
customer_name = Column(String(250), nullable=False)
delivery_type = Column(String(250), nullable=False)
并查看如下
class DeliveryDetailsView(ModelView, DeleteMixin):
datamodel = SQLAInterface(models.DeliveryDetails)
list_columns = ['customer_name','delivery_type']
search_columns = ['customer_name','delivery_type']
edit_columns = ['customer_name','delivery_type']
add_columns = edit_columns
label_columns = {
'customer_name': _("Customer Name"),
'delivery_type': _("Delivery Type") }
我想在下拉列表中将 Air, Land, Sea
显示为 Delivery Types
。请告诉我是否可以像我提到的那样做?
您可以使用 WTF forms。在您的 WTF 表单中使用以下字段:
dropdown_list = ['Air', 'Land', 'Sea'] # You can get this from your model
seqSimilarity = SelectField('Delivery Types', choices=dropdown_list, default=1)
或者:
如果您正在使用 jinja 模板并且想在没有 WTF 形式的情况下执行此操作,那么您可以将 dropdown_list
作为参数传递给 render_template()。最后简单地遍历列表并在 HTML.
中创建 select
在查看文件中:
@app.route("/url_you_want")
def view_method():
dropdown_list = ['Air', 'Land', 'Sea']
return render_template('your_template.html', dropdown_list=dropdown_list)
然后在your_template.html
<select>
{% for each in dropdown_list %}
<option value="{{each}}">{{each}}</option>
{% endfor %}
</select>
我是 flask 的新手,我想将下拉列表添加到具有预定义值(不是从数据库中获取)的表单中。我创建了一个模型如下。
class DeliveryDetails(Model):
_tablename_ = 'deliverydetails'
id = Column(Integer, primary_key=True)
customer_name = Column(String(250), nullable=False)
delivery_type = Column(String(250), nullable=False)
并查看如下
class DeliveryDetailsView(ModelView, DeleteMixin):
datamodel = SQLAInterface(models.DeliveryDetails)
list_columns = ['customer_name','delivery_type']
search_columns = ['customer_name','delivery_type']
edit_columns = ['customer_name','delivery_type']
add_columns = edit_columns
label_columns = {
'customer_name': _("Customer Name"),
'delivery_type': _("Delivery Type") }
我想在下拉列表中将 Air, Land, Sea
显示为 Delivery Types
。请告诉我是否可以像我提到的那样做?
您可以使用 WTF forms。在您的 WTF 表单中使用以下字段:
dropdown_list = ['Air', 'Land', 'Sea'] # You can get this from your model
seqSimilarity = SelectField('Delivery Types', choices=dropdown_list, default=1)
或者:
如果您正在使用 jinja 模板并且想在没有 WTF 形式的情况下执行此操作,那么您可以将 dropdown_list
作为参数传递给 render_template()。最后简单地遍历列表并在 HTML.
在查看文件中:
@app.route("/url_you_want")
def view_method():
dropdown_list = ['Air', 'Land', 'Sea']
return render_template('your_template.html', dropdown_list=dropdown_list)
然后在your_template.html
<select>
{% for each in dropdown_list %}
<option value="{{each}}">{{each}}</option>
{% endfor %}
</select>