flask-wtforms

flask wtforms selectfield choices not update

最后都变了- 提交于 2019-12-06 16:41:47
问题 class ArticleForm(Form): type = SelectField('type', choices=[(h.id, h.name) for h in ArticleType.query.all()], coerce=int) below is how I use the ArticleForm in views @admin.route('/article/add',methods=['get','post']) def article_create(): article_form = ArticleForm() my problem is that the selectField is not read the db each time I visit /article/add If I add a new type in the ArticleType the choice of the selectField will not update the choice until I restart the server. but If I use like

Pass a variable to a Flask WTForm

我们两清 提交于 2019-12-06 14:00:05
问题 I want to do a query select field with a default value that is passed in from the route. I can't figure out how to pass a variable from the View to the Form class class transactionsForm(Form): loan_id = QuerySelectField('trans_id', validators=[Required()], get_label='name', query_factory=lambda: trans.query.filter_by(trans_id=[MY VARIABLE]).all()) 回答1: This is from the QuerySelectField documentation: The query property on the field can be set from within a view to assign a query per-instance

upload file in ajax with wtforms

微笑、不失礼 提交于 2019-12-06 13:35:59
I use wtforms to handle forms. so i create form like this: class ProfileForm(Form): firstName = TextField(_('firstName'), [validators.Required(), validators.Length(min=3, max=45)]) lastName = TextField(_('lastName'), [validators.Required(), validators.Length(min=3, max=45)]) avatar = FileField(_('avatar'), [check_file]) this form work in simple upload fine ... but what about ajax ? is there any plugin to create iFrame or somethings to upload file via ajax? or i must handle this form in another way? ps: IE support be important ps2: i use wtform for another without file in ajax to. just by

Hiding a form-group with Flask Jinja2 and WTForms

限于喜欢 提交于 2019-12-06 10:23:10
I'm trying to show or hide a form field based on the state of a checkbox in another part of the form. I thought that I could do this with jQuery .show() or .hide() with relative ease, but I'm not having much luck so far. Any thoughts? The form class: class MyForm(Form): checked = BooleanField('Check this box:') date = DateField('Date:', format='%Y-%m-%d', id="dates") submit = SubmitField('Submit') The template: {% import "bootstrap/wtf.html" as wtf %} {% block content %} {{ form.hidden_tag() }} {{ wtf.form_field(form.checked) }} {{ wtf.form_field(form.date) }} {{ wtf.form_field(form.submit) }}

Setting a default on a select removes the settings passed in to populate the form

让人想犯罪 __ 提交于 2019-12-06 08:51:09
This code works fine separately. What I mean is when I set the default tag and call process() all the other data that should populate the form have been removed. In this case the default is ok, but the other fields are empty. form = ReviewForm(**populate_form) form.tags.default = '1' form.process() So, it seems like process cleans the **populate_form values out. I need to populate all fields and then set the default for the select. pswaminathan From the WTForms Documentation : Since BaseForm does not take its data at instantiation, you must call this to provide form data to the enclosed fields

Flask WTForms autofill StringField with variable

不想你离开。 提交于 2019-12-06 07:37:35
I have a form that I want to automatically fill some of the fields with information received on a previous page, but it needs to be changeable if they want to adjust it. I am using a dynamically created list for my SelectField that works, but adding the StringField has been unsuccessful. See my code below: forms.py class get_vals(var): ... typs = var.p_typs p_name = var.p_name return typs,p_name class NewForm(FlaskForm): name = StringField('Name', validators=[DataRequired()]) typ1 = SelectField('Type', validators=[Optional()]) def __init__(self,var): super(NewForm,self).__init__() typs,p_name

Flask self.errors.append() - AttributeError: 'tuple' object has no attribute 'append'

独自空忆成欢 提交于 2019-12-06 04:20:55
问题 My small registration app gives and error when I try to validate the submited data by user and check if the entered e-mail exists. here is my files: forms: from flask.ext.wtf import Form from wtforms import TextField, BooleanField, PasswordField, TextAreaField, validators from wtforms.validators import Required from app import models class RegisterForm(Form): """RegisterForm class needed for retrieving data from user""" username = TextField('username', [validators.Length(min=3, max=50),

Flask-WTForms: how to check if a field is required?

拟墨画扇 提交于 2019-12-06 04:20:19
问题 I defined a form in the following way: class LoginForm(Form): login = EmailField(u'Email address', [required(), length(min=5, max=2048), validators.Email()]) password = PasswordField(u'Password', [required(), length(min=6, max=50)]) next = HiddenField() remember = BooleanField('Remember me') submit = SubmitField('Login') Then I'm writing a generic macro in Jinja2 to render the form fields and I would like to do something like: {% if field.is_required() %} {{ field.label(class_='required') }}

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