flask-wtforms

How to use WTForms in Ajax validation?

空扰寡人 提交于 2019-12-17 22:35:57
问题 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 回答1: A possible solution is as follows: On the client side you attach a handler to the blur event

Why does a Flask app create two process? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-17 19:49:06
问题 This question already has answers here : Why does running the Flask dev server run itself twice? (5 answers) Closed 4 years ago . As far as I understood Flask should create a thread and a second thread to run on it, but what I see is there are always two processes, not threads, running. Even for the simplest app. from flask import Flask from flask import render_template, request, flash, session, redirect app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' app.run

WTForms-JSON not receiving form data

荒凉一梦 提交于 2019-12-14 04:09:38
问题 WTForms-JSON docs here. This has been a bug from hell I cannot wrap my head around. I have a simple API handler that works perfectly fine from Postman JSON POSTs. However when I plug the data into a web form the JSON is all empty. For example: 162.249.161.234 - - [23/May/2017 10:52:55] "POST /submitworkorder HTTP/1.1" 200 - {'customer': None} My Flask is as follows: @app.route('/') def index(): form = SubmitWorkorderForm.from_json(request.json) print form.data return render_template(

How to bind a field in __init__ function of a form

我的梦境 提交于 2019-12-13 17:24:50
问题 class Example_Form(Form): field_1 = TextAreaField() field_2 = TextAreaField() def __init__(self, type, **kwargs): super(Example_Form, self).__init__(**kwargs) if type == 'type_1': self.field_3 = TextAreaField() In some scenarios I need to dynamically add fields into the form. The field_3 added to example form turns out to be a UnboundField. I tried to bind field_3 to form in __init__ function, but it won't work. field_3 = TextAreaField() field_3.bind(self, 'field_3') How to bind field_3 to

Form Data not refreshing with newest entries

别来无恙 提交于 2019-12-13 07:52:22
问题 When I add a new customer in customer table and access AddOrderForm form, I don't get the new customer in the choices.But on server restart I am able to get the new customer in the choices list.Any reason ? Customer Table class Customer(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), unique=True) mobile_num = db.Column(db.String(13), unique=True) name = db.Column(db.String(120)) marketing_source = db.Column(db.String(120)) date_of_birth = db.Column(db

How to access WTForm fields in JQuery?

我与影子孤独终老i 提交于 2019-12-13 00:27:36
问题 I'm trying to make a website with Flask and am using WTForms for a registration page like so: views.py: @app.route('/register', methods=('GET', 'POST')) def register(): form = RegistrationForm() if form.validate_on_submit(): user = User(form.password.data) form.populate_obj(user) db.session.add(user) db.session.commit() login_user(user) return redirect(url_for('index')) return render_template('register.html', title='Register', form=form, user=current_user) register.html: {% extends "base.html

WTForm “OR” conditional validator? (Either email or phone)

我是研究僧i 提交于 2019-12-12 20:25:59
问题 class ContactForm(Form): name = StringField('Name', validators=[DataRequired(), Length(max=255)]) email = StringField('Email', validators=[Optional(), Email(), Length(max=255)]) phone = StringField('Phone number', validators=[Optional(), NumberRange(min=8, max=14)]) comment = TextAreaField(u'Comment', validators=[DataRequired()]) Is there anyway to specify a validator such that either email or phone is required? 回答1: You can create a validate method on the form and do some manual checking.

flask wtf SelectField, dynamic input and validation

戏子无情 提交于 2019-12-12 18:21:01
问题 First question: dynamic input into a SelectField (choices), in my Database (sqlite, with SqlAlchemy) I have a Table, and from this table I wont all entries in the choices from the SelectField. As the selected result I need the ID from the entry. foo_id = SelectField('Label', choices=[Foo.query.all()]) Second question: If I put this into the SelectField: foo_id = SelectField('Foo', choices=[(1, 'Foo 1'), (2, 'Foo 2')]) Every time: Not a valid choice What goes on with the validation? Thanks for

WTForms : How to add “autofocus” attribute to a StringField

落爺英雄遲暮 提交于 2019-12-12 13:34:40
问题 I am rather new to WTForms, Flask-WTF. I can't figure out how to simply add the HTML5 attribute "autofocus" to one of the form field, from the form definition. I would like to do that in the Python code, not in the Jinja template. Here is what I have : class NameForm(Form): name1 = StringField("Nom d'utilisateur :", validators=[Required(), Length(1, 16)]) pwd1 = PasswordField("Mot de passe :", validators=[Required(), Length(1, 16)]) mail1 = StringField("Compte GMail du calendrier :",

WTForms - dynamic labels by passing argument to constructor?

久未见 提交于 2019-12-12 11:18:57
问题 Is it possible to make my form labels dynamic by passing an argument to the form constructor? I'm thinking of something a bit like the following code: class MyForm(Form): def __init__(self, fruit): Form.__init__(self) self.fruit = fruit name = StringField('Do you like' + fruit + "?") @app.route('/' ,methods=["GET","POST"]) def home(): form = NameForm("bananas") Whatever I try, the text input box's label never seems to be able to access the variable 'fruit' - i.e. I can't seem to make the