wtforms

How to use WTForms in Ajax validation?

风格不统一 提交于 2019-11-28 18:24:37
I accustomed of using WTForms by means of Flask-WTF in my flask application. Doing server side validation is trivial. But how do I leverage this server validation to become a field level, ajax, client side validation? So, when user tab to another input fields, my application can directly goes on validating it and give validation warning/info/error. I haven't found a resource in the internet yet A possible solution is as follows: On the client side you attach a handler to the blur event in all the controls in the form. Each time the blur event occurs you run a Javascript function that collects

DatePickerWidget with Flask, Flask-Admin and WTforms

邮差的信 提交于 2019-11-28 17:54:25
问题 I'm trying to render a template that contains a DatePicker, but I'm getting a 500 error when I try. For my the code is correct, but it seems that something is failing or I'm not understanding correctly the way to do it. The code is the following: Reporting.py from flask.ext.admin import BaseView, expose from wtforms import DateField, Form from wtforms.validators import Required from flask.ext.admin.form import widgets from flask import request class DateRangeForm(Form): start_date = DateField

Filling WTForms FormField FieldList with data results in HTML in fields

江枫思渺然 提交于 2019-11-28 17:17:37
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 import DataRequired from werkzeug.datastructures import MultiDict app = Flask(__name__) app.config['SECRET

Recommendation for python form validation library [closed]

孤者浪人 提交于 2019-11-28 16:14:49
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I would like a form validation library that 1.separate html generation from form validation; 2.validation errors can be easily

How to populate wtform select field using mongokit/pymongo?

大城市里の小女人 提交于 2019-11-28 13:05:41
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/one/Projects/proj/app/__init__.py", line 27, in create_app from app.term.models import Term, TermCategory

How to send query results to a WTForm Field?

为君一笑 提交于 2019-11-28 11:43:13
问题 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

Add a CSS class to an option in a WTForms SelectField

点点圈 提交于 2019-11-28 10:17:19
问题 Can anyone please tell me how to assign css class to choices values. I would like to change the background of each choices with small image, so how can I do it with wtforms and css? class RegisterForm(Form): username = TextField('username', [validators.Length(min=3, max=50), validators.Required()]) img_url = SelectField('avatar', choices=[('static/images/avatars/1.jpg', '1'), ('static/images/avatars/2.jpg', '2'), ('static/images/avatars/3.jpg', '3'), ('static/images/avatars/4.jpg', '4'), (

WTForms: two forms on the same page?

纵然是瞬间 提交于 2019-11-28 09:21:24
I have a dynamic web-page that should process two forms: a login form and a register form . I am using WTForms to process the two forms but I am having some trouble making it work, since both forms are being rendered to the same page. The following is the code for the login form of my webpage: PYTHON: class Login(Form): login_user = TextField('Username', [validators.Required()]) login_pass = PasswordField('Password', [validators.Required()]) @application.route('/index', methods=('GET', 'POST')) def index(): l_form = Login(request.form, prefix="login-form") if request.method == 'POST' and l

Howto: Dynamically generate CSRF-Token in WTForms with Flask

一曲冷凌霜 提交于 2019-11-28 07:01:46
I have a fruits form that has one FieldList object for the bananas: bananas = FieldList(FormField(BananaForm)) In the frontend, initially, I add one of those fields to the FieldList form.append_entry() Now with Javascript I managed to create functions, that can dynamically add (plus button) or remove (minus button) the number of BananaForm fields that can be filled with information. FielstList automatically creates ids for all of its fields. So to do dynamical adding with js, I duplicate the HTML code and set the field id += 1, like: first field: <tr> <td><input id="bananas-0-originCountry"

How to use a WTForms FieldList of FormFields?

。_饼干妹妹 提交于 2019-11-28 06:33:08
I'm building a website using Flask in which I use WTForms . In a Form I now want to use a FieldList of FormFields as follows: class LocationForm(Form): location_id = StringField('location_id') city = StringField('city') class CompanyForm(Form): company_name = StringField('company_name') locations = FieldList(FormField(LocationForm)) so to give people the ability to enter a company with two locations (dynamic adding of locations comes later) I do this on the front side: <form action="" method="post" role="form"> {{ companyForm.hidden_tag() }} {{ companyForm.company_name() }} {{ locationForm