wtforms

Why does Flask WTForms and WTForms-SQLAlchemy QuerySelectField produce too many values to unpack?

回眸只為那壹抹淺笑 提交于 2019-11-26 21:43:41
问题 I've got a pretty basic Flask app: from flask import Flask, render_template from flask_wtf import FlaskForm from wtforms_sqlalchemy.fields import QuerySelectField from wtforms.validators import DataRequired from flask_sqlalchemy import SQLAlchemy from wtforms import StringField db = SQLAlchemy() app = Flask(__name__) app.config['SQLALCHEMY_URI'] = 'sqlite:///:memory:' app.config['SECRET_KEY'] = 'fnord' db.init_app(app) class Section(db.Model): id = db.Column(db.Integer, primary_key = True)

Add input fields dynamically with wtforms

你说的曾经没有我的故事 提交于 2019-11-26 19:17:14
问题 I'm not quite sure how approach this matter. I hope i get there. For example I have a table full of addresses on a page. The count of these are dynamic (could be 5 or 10 or any other count). And I want the possibility to edit them on one page. My approach was to create a Form with wtforms to edit one address and to multiply it in a jinja2 for loop and append to the html propertys name and id the loop.index0 from the itereation, so i can extract each row of data manually and put it back in my

Not a Valid Choice for Dynamic Select Field WTFORMS

a 夏天 提交于 2019-11-26 18:05:51
问题 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. 回答1: My guess is that Area.id is a int - when data comes back from the client it is treated as

Determine which WTForms button was pressed in a Flask view

心已入冬 提交于 2019-11-26 16:48:23
问题 I have a page with multiple links to redirect the user to different pages. I thought using a form would be nicer, so I defined a WTForms Form with multiple SubmitFields . How do I determine which button was clicked and redirect based on that? class MainForm(Form): user_stats = SubmitField('User Stats') room_stats = SubmitField('Room Stats') @main.route('/') @login_required def index(): form = MainForm() return render_template('index.html', form=form) <form action="#" method="post"> {{ wtf

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

*爱你&永不变心* 提交于 2019-11-26 13:28:38
问题 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

Clear valid form after it is submitted

流过昼夜 提交于 2019-11-26 12:47:16
问题 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) 回答1: The

Flask-WTF - validate_on_submit() is never executed

不问归期 提交于 2019-11-26 12:11: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\'

Flask view shows 400 error instead of template with form

浪子不回头ぞ 提交于 2019-11-26 08:37:47
问题 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>