<select> 如何在金家2中使用烧瓶-wtf?



我想在html"选择"字段中显示一个数据的"类别"。数据来自 view.py,数据的类别为"类别2"。我使用 jinjia2 来显示 html:

render_temperlate(data=data,form=form,'data.html')

然后我在 form.py 中定义一个烧瓶-wtf 选择字段,如下所示:

Class  CategoryForm(FlaskForm):
    category = SelectField(choices=[('','') , ("category 1","category 1"),("category2","category2")])

现在,我想在 web html "select" 标签中显示我的数据的类别(它的值是"capetory2"(。

在数据.html文件(金家2(,我尝试使用

{{form.category(option=data.category)}}

显示数据的类别(类别 2(,但在 Web 中,该字段没有任何内容。

我该怎么办?

如果在创建时将值传递给表单,则在呈现后将在HTML表单中选择该值,则无需将其作为选项传递。

import flask
from flask_wtf import FlaskForm
from jinja2 import Template
from wtforms import SelectField
class CategoryForm(FlaskForm):
    category = SelectField(choices=[('','') , ("category 1","category 1"),("category 2","category 2")])
    class Meta:
        csrf = False
def index():
    cf = CategoryForm(category="category 1")
    return flask.render_template(
        Template("""
                <html>
                <body>
                    <form method="POST">
                        {{form.category()}}
                        <input type="submit">
                    </form>
                </body>
                </html>
                """),
        form=cf)

最新更新