wtforms

How to send query results to a WTForm Field?

做~自己de王妃 提交于 2019-11-29 17:46:38
I use SQLalchemy with a many to many table to manage blog post tags. I need help rendering the tag values into a TextArea form field where they can be edited. Right now when I render I see the lookup query. Model The relationship between Tag and Post is is defined in `tags' class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), unique=True) url = db.Column(db.String(120), unique=True) def __init__(self, name, url): self.name = name self.url = url class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80)) body =

Setting data attributes on a WTForms field

余生长醉 提交于 2019-11-29 14:15:08
问题 I want to add "data-" attributes to a form field for integration with Bootstrap. I tried the following in a template: {{ form.test(data-toggle="toggle", data-size="mini", data-on="Yes", data-off="No", type="checkbox")}} and got this error: TemplateSyntaxError: expected token ',', got '=' Why did I get this error and how do I fix it? 回答1: You need to use valid Python names as the variable names. Therefore names like "data-toggle" are invalid because they have a "-" in them. Change the names to

How to dynamically set default value in WTForms RadioField?

江枫思渺然 提交于 2019-11-29 13:49:45
问题 I'm building a website with the Python Flask framework in which I use WTForms. In one form I've got a RadioField defined as follows: display = RadioField('display', default='ONE') This does not have any choices defined, because I do that lateron (which works perfectly fine): myForm = MyForm() myForm.display.choices = [('ONE', 'one'), ('TWO', 'two')] # This works fine I now want to set the default value for the RadioField after I set the choices for it. So I tried removing the default value

Get selected text from a form using wtforms SelectField

为君一笑 提交于 2019-11-29 10:48:40
This is a question upon the use of wtforms SelectField . Once the form submitted, I wish to extract selected text. I have the following form: from wtforms import Form, SelectField class TestForm(Form): hour = SelectField(u'Hour', choices=[('1', '8am'), ('2', '10am') ]) Here's the view: @app.route('/', methods=['GET', 'POST']) def test_create(): form =TestForm(request.form) if request.method == 'POST' and form.validate(): test = Test() form.populate_obj(test) test.hour=form.hour.name db.session.add(test) db.session.commit() return redirect(url_for('test_create')) return render_template('test

SelectField使用方法数据库查询使用

若如初见. 提交于 2019-11-29 10:19:49
fields.SelectField可供使用的数据来源不仅可以是预定义的,也可以是从数据库中查询。 tasks = [(r.task_name, r.task_name) for r in db.session.query(Task).all()] task = fields.SelectField(label=u'发布任务',validators = [validators.required()], choices = tasks) 这样就可以很方便的从数据库中查询到所有的记录,并作为下拉列表框中的选项供使用者选择。 使用SelectField存在的问题 不过严格来说,上述的场景并不是很适合用SelectField。因为在使用过程中会发现即使Task中的数据一直在更新而下拉列表框中的记录永远不变。 主要原因是task是表单类中的一个静态成员,定义之后就保持不变。因此,即使Task表中的数据一直在变,但是tasks的结果是已经固定的。 使用QuerySelectField 解决的办法还是有的,就是使用QuerySelectField,QuerySelectField并不在wtforms.fields文件中,所以导致我们我们并不知道它的存在。而且wtforms的官网也推荐我们 用SelectField支持从数据库中获取记录,导致我们误以为可以那么使用

Validate WTForm form based on clicked button

百般思念 提交于 2019-11-29 08:48:22
On my form, I have two buttons that I use for submitting the form. One button deletes selected files (presented in a table, one checkbox to an object) and the other selects them for processing. When the files are selected on deletion, no validation is necessary (beyond checking that at least one file has been selected). However, for processing I need to make sure that there is exactly one file of a certain extension. Basically, I need different validation processes based on which button the user clicked. How can I best do this? I know I can perform validation in the view, but I would much

Using FieldList and FormField

*爱你&永不变心* 提交于 2019-11-29 06:53:03
问题 We have the following forms and we are trying to create list of GroupRoleForms for each group. class FullNameMixIn(): full_name = TextField( 'Full name', [ validators.required(message=u"Full name is required") ]) class GroupRoleForm(Form): group =BooleanField('Group', default=False) role = SelectField( 'Role',choices=[ ("none", "----------"), ('approver', 'Approver'), ('editor', 'Editor') ]) class AdminEditUserForm(Form, FullNameMixIn): group_roles = FieldList(FormField(GroupRoleForm)) How

WTForm: FieldList with SelectField, how do I render?

左心房为你撑大大i 提交于 2019-11-29 03:20:58
问题 I have this order form which allows my users to create an order. An order consists of multiple tuples of (producetype, quantity) . Producetype should be rendered in a <select> form while quantity can just be an input. The choices of producetype should be dynamically added because that could change. Currently, I've written this in bare html I would like to use WTForm for this because WTForm really simplifies my code. However, I am unable to do so: Code: class OrderEntryForm(Form): quantity =

WTForms support for input readonly attribute?

家住魔仙堡 提交于 2019-11-28 23:17:29
Here they say it's not supported out of the box. Do you know a way to make HTML input form fields use the 'readonly' attribute with WTForms? I assume you are talking about the <input readonly> attribute in HTML/XHTML, which is not what that discussion thread you linked is about. (the linked thread is about a lower-level issue with how to ignore passed form input) The way to set a readonly attribute (and indeed any attribute on a field) is as a keyword-arg in your template. If using Jinja, this looks like (html5): {{ form.myfield(readonly=true) }} And for XHTML or versions of WTForms older than

Dynamic select field using WTForms not updating

妖精的绣舞 提交于 2019-11-28 22:10:52
I'm trying to make a dynamic select field using wtforms and sqlalchemy, but it doesn't update when an item is inserted or deleted from the database. Here's my code: class UserForm(Form): username = StringField('Username', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()]) job = SelectField( 'Job', validators=[DataRequired()], choices=[(a.id, a.name) for a in Job.query.order_by(Job.name)] ) And the database model: class Job(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) def __init__(self, name):