同一个表单字段的多个实例

编程入门 行业动态 更新时间:2024-10-21 10:15:48
本文介绍了同一个表单字段的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

类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) 其中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)

更多推荐

同一个表单字段的多个实例

本文发布于:2023-11-24 22:39:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字段   表单   实例

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!