wtforms

Make WTForms set field label from database model

匆匆过客 提交于 2019-12-07 07:41:53
问题 I have three tables: components , attributes and attribute_values . Each component can have many attribute_values . Each attribute_value belongs to one attribute . Yeah, it's the dreaded EAV pattern... I have created these two forms: class AttributeValueForm(Form): attribute = HiddenField() value = StringField('Value') class ComponentForm(Form): ... non-related fields left out ... attribute_values = FieldList(FormField(AttributeValueForm)) These are the SQLAlchemy models: class Component(db

WTForms doesn't validate - no errors

岁酱吖の 提交于 2019-12-07 00:43:56
问题 I got a strange problem with the WTForms library. For tests I created a form with a single field: class ArticleForm(Form): content = TextField('Content') It receives a simple string as content and now I use form.validate() and it returns False for any reason. I looked into the validate() methods of the 'Form and Field object. I found out that the field returns true if the length of the errorlist is zero. This is true for my test as i don't get any errors. In the shell the validation of my

Wtforms: How to generate blank value using select fields with dynamic choice values

二次信任 提交于 2019-12-06 22:01:14
问题 I'm using Flask with WTForms (doc) on Google App Engine. What is the best way to generate an field with an empty value for a select field? form.group_id.choices = [(g.key().id(), g.name) for g in Group.all().order('name')] Is there something like "blank=True" for the form field? myfield = wtf.SelectField() 回答1: Can you just prepend an empty pair to the list? form.group_id.choices.insert(0, ('', '')) 回答2: If it's a QuerySelectField , you can add parameters like this: allow_blank=True, blank

Pre-populating a BooleanField as checked (WTForms)

北城以北 提交于 2019-12-06 21:20:18
问题 For the life of me, I can't figure out how to pre-populate a BooleanField with WTForms. I have a field called "active". It defaults to being not checked, and it's not required. So I set it up like... class QuestionForm(Form): question = TextField('Question', [validators.Required()]) slug = TextField('Slug', [validators.Required()]) active = BooleanField('Active') Then I have an EDIT PAGE where I display a form for the 'question' I want to edit. {{ form.question.label }} {{ form.question(value

Regex validation with WTForms and python

廉价感情. 提交于 2019-12-06 19:44:24
问题 Here is my code: class CreateUser(Form): username = StringField('Username', [ validators.Regexp('\w+', message="Username must contain only letters numbers or underscore"), validators.Length(min=5, max=25, message="Username must be betwen 5 & 25 characters") ]) password = PasswordField('New Password', [ validators.DataRequired(), validators.EqualTo('confirm', message='Passwords must match') ]) confirm = PasswordField('Repeat Password') So the problem exists at line 3. I want the username to be

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

Flask-Admin: How to change model using on_model_change?

空扰寡人 提交于 2019-12-06 12:47:24
I'm trying to set a field's value to be based on another field in the same form: def on_model_change(form, model, is_created): model.textcolumn.data = model.textcolumn2.data Updating via the Flask-Admin interface raises no exceptions, but no changes were made to the value in model.textcolumn. Inspecting the "model" object, I also noticed this is not the same as the SQLAlchemy model used to generate the ModelView. How can I change model.textcolumn's value to model.textcolumn2's value? Is there a way to access the SQLAlchemy model object directly? This would be much better. I did it using the

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-wtf selectField choices not valid

烈酒焚心 提交于 2019-12-06 04:47:22
I made a SelectField like this: # constants.py QUESTION_LIST = {} QUESTION_LIST['QuestionOne'] = { 'disagree-strong': "Strongly Disagree", 'agree-strong': "Strongly Agree" } #forms.py from constants import * typeone = SelectField('QuestionOne', choices=QUESTION_LIST['QuestionOne'].iteritems(), description='Answer the question') So when you load the page, it shows the choices. I pick the choice, press submit and it says "this is not a valid choice" and it blanks out the select field. Then when you refresh the page, it's as if the code is broken and it doesn't show choices anymore... It's an