wtforms

How do you set a default value for a WTForms SelectField?

一个人想着一个人 提交于 2019-11-27 07:33:54
When attempting to set the default value of a SelectField with WTForms, I pass in value to the 'default' parameter like so. class TestForm(Form): test_field = SelectField("Test: ", choices=[(1, "Abc"), (2, "Def")], default=2) I have also tried the following. class TestForm(Form): test_field = SelectField("Test: ", choices=[(1, "Abc"), (2, "Def")], default=(2, "Def")) Neither set the default selected field to "Def." This works for other kinds of Fields such as TextField. How do you set the default value for a SelectField?' The first way you posted is correct, and it works for me. The only

Flask-WTF - validate_on_submit() is never executed

给你一囗甜甜゛ 提交于 2019-11-27 06:49:25
I'm using Flask-WTF: Here is my form: from flask.ext.wtf import Form, TextField class BookNewForm(Form): name = TextField('Name') Here is the controller: @book.route('/book/new', methods=['GET', 'POST']) def customers_new(): form = BookNewForm() if form.is_submitted(): print "submitted" if form.validate(): print "valid" if form.validate_on_submit(): flash("Successfully created a new book") return redirect(url_for('.books_show')) return render_template('views/books_new.html', form=form) Now the problem is, if you look at my print statements, it always prints submitted, but it NEVER prints valid

How do I validate wtforms fields against one another?

人走茶凉 提交于 2019-11-27 05:54:41
问题 I have three identical SelectField inputs in a form, each with the same set of options. I can't use one multiple select. I want to make sure that the user selects three different choices for these three fields. In custom validation, it appears that you can only reference one field at a time, not compare the value of this field to others. How can I do that? Thanks! 回答1: You can override validate in your Form ... class MyForm(Form): select1 = SelectField('Select 1', ...) select2 = SelectField(

WTForms: two forms on the same page?

こ雲淡風輕ζ 提交于 2019-11-27 05:50:56
问题 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'

Form is never valid with WTForms

不打扰是莪最后的温柔 提交于 2019-11-27 05:40:51
I have a Flask-WTF form for sign in. Apparently the form is never valid, no matter what I enter "success" is never printed. Why isn't my form validating? class loginForm(Form): email = EmailField('email', validators=[InputRequired("Please enter your email address."), Email("Please enter a valid email address.")]) password = PasswordField('password', validators=[InputRequired("Please enter your password.")]) @app.route('/sign-in', methods=['POST', 'GET']) def signIn(): form = loginForm(request.form) if form.validate_on_submit(): print 'success' return redirect('/') return render_template(

Clear valid form after it is submitted

前提是你 提交于 2019-11-27 05:18:32
I want to reset the form after it validates. Currently the form will still show the previous data after it is submitted and valid. Basically, I want the form to go back to the original state with all fields clean. What is the correct to do this? @mod.route('/', methods=['GET', 'POST']) def home(): form = NewRegistration() if form.validate_on_submit(): #save in db flash(gettext(u'Thanks for the registration.')) return render_template("users/registration.html", form=form) The issue is that you're always rendering the form with whatever data was passed in, even if that data validated and was

Add a css class to a field in wtform

不羁的心 提交于 2019-11-27 03:58:51
问题 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)

Howto: Dynamically generate CSRF-Token in WTForms with Flask

不打扰是莪最后的温柔 提交于 2019-11-27 01:45:54
问题 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

How do I generate dynamic fields in WTForms

和自甴很熟 提交于 2019-11-27 01:20:52
问题 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

Flask view shows 400 error instead of template with form

耗尽温柔 提交于 2019-11-26 23:19:45
I'm trying to display a page with a form, then add a Player to the database when the form is submitted. However, I can't view the form because the browser always shows a 400 Bad Request error. Other posts indicate that this could be because the name of the form input doesn't match the key I get from request.form , but all my keys match. Why do I get this error? <form method="post"> {{ form.hidden_tag() }} <input name="name"> <input name="available"> <input type="submit"> </form> @app.route('/addplayer', methods=['GET', 'POST']) def addplayer(): connect('basketball_contracts', host='localhost',