flaskform传递变量(WTForms) [英] flaskform pass a variable (WTForms)
问题描述
我想传递一个str用作表单提示.我认为这很简单,但事实证明很难.
I want to pass a str to be used as the prompt for a form. I thought it would be simple but it is proving to be difficult.
这是我的代码:
class PostForm(FlaskForm):
post = TextAreaField(Question, validators=[DataRequired()])
submit = SubmitField('Submit')`
然后
form = PostForm('my question')
相应的html
{{ wtf.quick_form(form) }}
推荐答案
今天,我发现了一个与您类似的问题.我想将变量传递给 FlaskForm
.对于一个小型的简历创建应用程序,我想给用户一个机会来创建多个他的工作经历条目,并且我想使用 FieldList
和 FormField
来实现.而且我需要以一种形式在一页上完成它.
Today I'm figured out about a similar problem as yours. I wanted to pass a variable to FlaskForm
. For a small CV creation app, I want to give the user an opportunity to create more than 1 entry for his work experience and I wanted to do it with FieldList
and FormField
. And I needed to do it on one page so in one form.
我的解决方案是表单的工厂模式的非常简单的python实现:
My solution is pretty simple python implementation of factory pattern for forms:
class ConstructorForm(FlaskForm):
...
work_experience_form = FieldList(FormField(WorkExperienceForm), min_entries=1, max_entries=1)
skills_form = FieldList(FormField(SkillsForm), min_entries=1, max_entries=1)
...
这是我构建扩展表单的功能:
And here is my function for building extending forms:
def constructor_form(work_experience_forms=1, skills_forms=1):
class _ConstructorForm(ConstructorForm):
pass
_ConstructorForm.work_experience_form = FieldList(
FormField(WorkExperienceForm), min_entries=work_experience_forms, max_entries=work_experience_forms
)
_ConstructorForm.skills_form = FieldList(
FormField(SkillsForm), min_entries=skills_forms, max_entries=skills_forms
)
return _ConstructorForm()
这篇关于flaskform传递变量(WTForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!