wtforms

Choices validation in WTForms does not update when database does

点点圈 提交于 2019-12-06 02:49:17
问题 I understand the SelectField method in WTForms takes can argument choices which has the form... choices=[("value1", "display of value 1"), ("value2", "display of value 2")] I need to populate my choices based on a call to the database. I'm using neo4j as my backend, so I can't use modelforms or the other built-in solutions for populating data in a form. def get_list_of_things(): query = 'start n = node:things("*:*") return ID(n), n.display_name;' t = cypher.execute(cdb, query)[0] for x in t:

How to override the html default “Please fill out this field” when validation fails in Flask?

喜你入骨 提交于 2019-12-06 01:49:33
问题 I have a Flask-WTF form with with DataRequired validators and related error messages. However, the Chrome browser seems to ignore my error messages and just displays "Please fill out this field". How do I get Flask to override this and show my messages? from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, SubmitField from wtforms.validators import DataRequired class SignupForm(FlaskForm): first_name = StringField('First Name', validators=[DataRequired(message='Hey,

Select2 field implementation in flask/flask-admin

谁说我不能喝 提交于 2019-12-05 23:48:03
问题 I'm trying to implement Select2 field in one of my flask views. Basically I want the same select2 field in my flask application view (not a flask admin modelview) as in Flask-admin model create views. Currently my solution has been QuerySelectField from wtforms that looks something like this class TestForm(Form): name= QuerySelectField(query_factory=lambda: models.User.query.all()) This allows me to load and select all the data I need, but it does not provide select2 search box etc. Currently

Using WTForms' populate_obj( ) method with Flask micro framework

偶尔善良 提交于 2019-12-05 22:35:39
问题 I have a template which allows the user to edit their user information. <form method="post"> <table> <tr> <td>Username:</td> <td>{{user['username']}}</td> </tr> <tr> <td>New Password:</td> <td> <input type="password" name="password"></td> <td>{% if form.password.errors %} {{form.password.errors}} {% endif %}<td> </tr> <tr> <td>Re-enter Password:</td> <td> <input type="password" name="confirm_password"> </td> </tr> <input type='hidden' name='username' value="{{user['username']}}"> <tr> <td>

Make WTForms set field label from database model

为君一笑 提交于 2019-12-05 16:15:13
I have three tables: components , attributes and attribute_values . Each component can have many attribute_values . Each attribute_value belongs to one attribute . Yeah, it's the dreaded EAV pattern... I have created these two forms: class AttributeValueForm(Form): attribute = HiddenField() value = StringField('Value') class ComponentForm(Form): ... non-related fields left out ... attribute_values = FieldList(FormField(AttributeValueForm)) These are the SQLAlchemy models: class Component(db.Model): __tablename__ = 'components' id = db.Column(db.Integer, primary_key=True) ... non-related

WTForms-JSON not working with FormFields

跟風遠走 提交于 2019-12-05 14:18:36
Nested forms (FormFields) doesn't get populated with data when I use WTForms-JSON. I can't spot my mistake, see example below. from flask import Flask, request, jsonify from flask_wtf import Form from wtforms import TextField, FormField, IntegerField from wtforms.validators import InputRequired import wtforms_json app = Flask(__name__) app.config["WTF_CSRF_ENABLED"] = False wtforms_json.init() class Address(Form): street = TextField('street', validators=[InputRequired()]) number = IntegerField('number', validators=[InputRequired()]) class User(Form): name = TextField('name', validators=

Setting default value after initialization in SelectField flask-WTForms

荒凉一梦 提交于 2019-12-05 14:09:37
I would like to pre-select the value for SelectField when displaying it to user. default argument works when it is passed at the time of instantiation but does not work once the field is initialized. class AddressForm(Form): country = SelectField('Country',choices=[('GB', 'Great Britan'), ('US', 'United States')], default='GB') # works When I try to use default value to preselect option before presenting the form to user for editing, it doesn't work. address_form = AddressForm() address_form.country.default='US' # doesnot work Need a solution to set default value to pre-set values before

Wtforms: adding dynamic fields with multiple inheritance

眉间皱痕 提交于 2019-12-05 08:24:05
I know I can create dynamic fields like this: http://wtforms.simplecodes.com/docs/1.0.1/specific_problems.html#dynamic-form-composition But the above solution is unwieldy in my case, and requires a special API which I would like to avoid. I am wondering if there is a way to get this working with multiple inheritance? I tried the following and it won't work and I don't know why, I figured that WTForms should bind the forms properly given how the class structure is working: >>> class Base(Form): ... def __init__(self, **kwargs): ... setattr(self, 'dynamic_boolean', fields.BooleanField('label'))

Wtforms: How to generate blank value using select fields with dynamic choice values

十年热恋 提交于 2019-12-05 03:39:08
I'm using Flask with WTForms ( doc ) on Google App Engine. What is the best way to generate an field with an empty value for a select field? form.group_id.choices = [(g.key().id(), g.name) for g in Group.all().order('name')] Is there something like "blank=True" for the form field? myfield = wtf.SelectField() Can you just prepend an empty pair to the list? form.group_id.choices.insert(0, ('', '')) moogoo If it's a QuerySelectField , you can add parameters like this: allow_blank=True, blank_text=u'-- please choose --' 来源: https://stackoverflow.com/questions/5868718/wtforms-how-to-generate-blank

wtforms raise a validation error after the form is validated

痞子三分冷 提交于 2019-12-05 03:31:29
I have a registration form that collects credit card info. The workflow is as follows: User enters registration data and card data through stripe. The form is validated for registration data. If the form is valid, payment is processed. If payment goes through, it's all good, the user is registered and moves on. If the payment fails, i want to be able to raise a validation error on a hidden field of the form. Is that possible? Here's a the form submission code: def register(): form = RegistrationForm() if form.validate_on_submit(): user = User( [...] ) db.session.add(user) #Charge amount =