wtforms

How do I generate dynamic fields in WTForms

我的梦境 提交于 2019-11-28 06:27:38
I am trying to generate a form in WTForms that has dynamic fields according to this documentation http://wtforms.simplecodes.com/docs/1.0.2/specific_problems.html#dynamic-form-composition I have this subform class which allows users to pick items to purchase from a list: class Item(Form): itmid = SelectField('Item ID') qty = IntegerField('Quantity') class F(Form): pass There will be more than one category of shopping items, so I would like to generate a dynamic select field based on what categories the user will choose: fld = FieldList(FormField(Item)) fld.append_entry() but I get the

How to make a field conditionally optional in WTForms?

╄→尐↘猪︶ㄣ 提交于 2019-11-28 03:42:00
My form validation is working nearly complete, I just have 2 cases I don't know exactly how to solve: 1) The password field should be required of course but I also provide the possibility to log in with google or facebook account via OAuth and then name gets prefilled but I remove the password field completely from the form is there is a user (google) or a facebook user object: <tr><td> <br /> {% if user or current_user %} {% else %} <div class="labelform"> {% filter capitalize %}{% trans %}password{% endtrans %}{% endfilter %}: </div> </td><td> <div class="adinput">{{ form.password|safe }}{%

Validate WTForm form based on clicked button

…衆ロ難τιáo~ 提交于 2019-11-28 02:10:24
问题 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

Not a Valid Choice for Dynamic Select Field WTFORMS

百般思念 提交于 2019-11-27 18:31:04
I currently am creating a dynamic select field using WTFORMS, however it never submits and fails the validation with the following error. Not a valid choice My Field is created like this: area = SelectField() and in the view, i am grabbing the options from the db like so: form = MytestForm() form.area.choices = [(a.id, a.name) for a in Area.objects.all()] It works however if i create static options. My guess is that Area.id is a int - when data comes back from the client it is treated as a string by WTForms unless a callable is passed to the coerce keyword argument of the wtforms.fields

Add a css class to a field in wtform

冷暖自知 提交于 2019-11-27 17:23:42
I'm generating a dynamic form using wtforms (and flask). I'd like to add some custom css classes to the fields I'm generating, but so far I've been unable to do so. Using the answer I found here , I've attempted to use a custom widget to add this functionality. It is implemented in almost the exact same way as the answer on that question: class ClassedWidgetMixin(object): """Adds the field's name as a class. (when subclassed with any WTForms Field type). """ def __init__(self, *args, **kwargs): print 'got to classed widget' super(ClassedWidgetMixin, self).__init__(*args, **kwargs) def __call__

Dynamic select field using WTForms not updating

此生再无相见时 提交于 2019-11-27 14:15: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

WTForms support for input readonly attribute?

心不动则不痛 提交于 2019-11-27 13:46:21
问题 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? 回答1: 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

Python: WTForms Can I add a placeholder attribute when I init a field?

守給你的承諾、 提交于 2019-11-27 11:26:01
问题 I want to add a placeholder attribute on to the field in WTForms. How can I do it? abc = TextField('abc', validators=[Required(), Length(min=3, max=30)], placeholder="test") The above code is not valid How can I add a placeholder attribute with value? 回答1: Updated for WTForms 2.1 You can now as of WTForms 2.1 (December 2015) set rendering keywords by using the render_kw= parameter to the field constructor. So the field would look like: abc = StringField('abc', [InputRequired()], render_kw={

Filling WTForms FormField FieldList with data results in HTML in fields

回眸只為那壹抹淺笑 提交于 2019-11-27 10:23:18
问题 I have a Flask app in which I can populate form data by uploading a CSV file which is then read. I want to populate a FieldList with the data read from the CSV. However, when I try to populate the data, it enters raw HTML into the TextFields instead of just the value that I want. What am I doing wrong? app.py from flask import Flask, render_template, request, url_for from flask.ext.wtf import Form from wtforms import StringField, FieldList, FormField, SelectField from wtforms.validators

How to populate wtform select field using mongokit/pymongo?

萝らか妹 提交于 2019-11-27 07:34:23
问题 I'm trying to create a SelectField using a mongodb query, but so far I haven't been successful: # forms.py in blueprint CATEGORIES = [] for item in db.Terms.find(): CATEGORIES.append((item['slug'], item['name'])) class TermForm(Form): category = SelectField( choices=CATEGORIES, validators=[Optional()]) But I get an exception: Traceback (most recent call last): File "/home/one/Projects/proj/manage.py", line 14, in <module> app = create_app(os.getenv('FLASK_CONFIG') or 'default') File "/home