在 Flask WTForms 中使用 RadioField 公开附加表单字段
Exposing Additional Form fields with RadioField in Flask WTForms
刚开始使用 WTForms,我不知道如何公开其他表单字段,如下所示:https://css-tricks.com/exposing-form-fields-radio-button-css/
因为单选字段有提供字段列表的选项参数,有什么方法可以自定义每个选项吗?喜欢在选择不同的选项时展现不同的东西?
这将是 python 和 javascript 代码的混合。
有2种常见解法:
- 一切都已在网页中,未使用的字段被隐藏,直到用户执行操作(在单选按钮上)。
- 进行 API 调用以获得动态自定义选择。
您可以创建路线:
@app.route("/api/choices", methods=["GET"])
def my route:
custom_choices = get_custom_choices()
return jsonify(custom_choices)
然后在您的 javascript 代码中,当用户单击按钮时,您向此路由发出请求以获取自定义选项。
然后当您 check/validate 发送表单时,使用自定义选项(在示例中,get_custom_choice)填充字段的选项,如 WTForms 文档中所述:
Note that the choices keyword is only evaluated once, so if you want
to make a dynamic drop-down list, you’ll want to assign the choices
list to the field after instantiation. Any submitted choices which are
not in the given choices list will cause validation on the field to
fail. If this option cannot be applied to your problem you may wish to
skip choice validation (see below).
Select fields with dynamic choice values:
class UserDetails(Form):
group_id = SelectField('Group', coerce=int)
def edit_user(request, id):
user = User.query.get(id)
form = UserDetails(request.POST, obj=user)
form.group_id.choices = [(g.id, g.name) for g in Group.query.order_by('name')]
Note we didn’t pass a choices to the SelectField constructor, but
rather created the list in the view function. Also, the coerce keyword
arg to SelectField says that we use int() to coerce form data. The
default coerce is str().
第二种解决方案需要更多的工作,但它更轻松,具体取决于您有多少种不同的选择可能性。 (在我看来:更清洁)。
刚开始使用 WTForms,我不知道如何公开其他表单字段,如下所示:https://css-tricks.com/exposing-form-fields-radio-button-css/
因为单选字段有提供字段列表的选项参数,有什么方法可以自定义每个选项吗?喜欢在选择不同的选项时展现不同的东西?
这将是 python 和 javascript 代码的混合。
有2种常见解法:
- 一切都已在网页中,未使用的字段被隐藏,直到用户执行操作(在单选按钮上)。
- 进行 API 调用以获得动态自定义选择。
您可以创建路线:
@app.route("/api/choices", methods=["GET"])
def my route:
custom_choices = get_custom_choices()
return jsonify(custom_choices)
然后在您的 javascript 代码中,当用户单击按钮时,您向此路由发出请求以获取自定义选项。
然后当您 check/validate 发送表单时,使用自定义选项(在示例中,get_custom_choice)填充字段的选项,如 WTForms 文档中所述:
Note that the choices keyword is only evaluated once, so if you want to make a dynamic drop-down list, you’ll want to assign the choices list to the field after instantiation. Any submitted choices which are not in the given choices list will cause validation on the field to fail. If this option cannot be applied to your problem you may wish to skip choice validation (see below).
Select fields with dynamic choice values:
class UserDetails(Form): group_id = SelectField('Group', coerce=int) def edit_user(request, id): user = User.query.get(id) form = UserDetails(request.POST, obj=user) form.group_id.choices = [(g.id, g.name) for g in Group.query.order_by('name')]
Note we didn’t pass a choices to the SelectField constructor, but rather created the list in the view function. Also, the coerce keyword arg to SelectField says that we use int() to coerce form data. The default coerce is str().
第二种解决方案需要更多的工作,但它更轻松,具体取决于您有多少种不同的选择可能性。 (在我看来:更清洁)。