问题描述
类InviteForm(Form):
person = TextField(person,validators = [validators.Required(Please enter persons name。)])
email = EmailField(email,validators = [validators.Required(请输入valid电子邮件(请输入有效的电子邮件。)))
def validate(self):
return validate_form(self)
$ c
其中validate_form函数是一个cusotm验证器,它检查几个邀请条件。
我的要求是允许用户一次邀请多个人。为了实现这一点,我添加了jQuery的功能,它以html的形式复制这些字段,并允许邀请多个人。
但是这个问题是在我的视图函数中,当我从后期请求中提取结果时,只给出第一个人的信息。我怎样才能得到所有的人的细节。我的观点定义如下:
$ $ p $ @ app.route(/ invite,methods = [GET,POST ))
def invite():
invite_form = InviteForm()
if invite_form.validate_on_submit():
打印invite_form.person
打印invite_form.email
这只给出一个字段,而不是字段数组。
这可能与python wtf?如何?
你要找的是 FormField
你建立一个WTF字段(甚至组)的列表。
下面是一个简单的例子 - 它将呈现三个字符串字段作为html列表,因为这是最小的需要。如果你想通过javascript添加附加组件,那么只要坚持使用相同的命名方案,在下面的例子中,第四个组件的名字就是 person-3
。
from flask导入Flask,render_template_string $ b $ from flask.ext.wtf从wtforms导入表单
import FieldList,StringField
app = Flask(__ name__)
app.secret_key ='TEST'
TestForm(Form):
person = FieldList( StringField('Person'),min_entries = 3,max_entries = 10)
foo = StringField('Test')
$ b $ app_route('/',methods = ()')
def example():
form = TestForm()
if form.validate_on_submit():
print form.person.data
## [u'One',u'Two',u'Three']
return render_template_string(
< form method =post action =#>
{{form.hidden_tag()}}
{{form.person}}
< input type =submit/>
< / form>
,form = form)
$ b $ if if __name__ =='__main__':
app.run(debug = True)
I have invite form with two fields defined as person and email as follows:
class InviteForm(Form):
person = TextField("person", validators=[validators.Required("Please enter persons name.")])
email = EmailField("email", validators=[validators.Required("Please enter valid email."), validators.Email("Please enter valid email.")])
def validate(self):
return validate_form(self)
Where validate_form function is a cusotm validator which check few conditions for invite.
My requirement is to allow users to invite more than one person at a time. To achieve this I have added jquery function which replicates these fields in html form and allow to invite multiple people.
But the problem is in my view function when I extract results from post request it gives only first persons information. How can I get all the persons details. My view is defined as follows:
@app.route("/invite", methods=["GET", "POST"])
def invite():
invite_form = InviteForm()
if invite_form.validate_on_submit():
print invite_form.person
print invite_form.email
This gives only one field, instead of array of fields.
Is this possible with python wtf? How?
What you're looking for is FormField
which lets you build a list of the WTF Fields (or groups even).
Here's a simple example-- it'll render three string fields as a html list because that's the minimum required. If you want to add extras via javascript, then just adhere to the same naming scheme, in the case below, a fourth would have a name of person-3
.
from flask import Flask, render_template_string
from flask.ext.wtf import Form
from wtforms import FieldList, StringField
app = Flask(__name__)
app.secret_key = 'TEST'
class TestForm(Form):
person = FieldList(StringField('Person'), min_entries=3, max_entries=10)
foo = StringField('Test')
@app.route('/', methods=['POST', 'GET'])
def example():
form = TestForm()
if form.validate_on_submit():
print form.person.data
## [u'One', u'Two', u'Three']
return render_template_string(
"""
<form method="post" action="#">
{{ form.hidden_tag() }}
{{ form.person }}
<input type="submit"/>
</form>
""", form=form)
if __name__ == '__main__':
app.run(debug=True)
这篇关于同一个表单字段的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!