wtforms

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.

Long sorted dropdown list with WTForms

余生长醉 提交于 2019-12-12 20:00:00
问题 I'd like to create a drop-down list of the U.S. states in alphabetical order. I've converted a tuple of states into an OrderedDict and I'm feeding this into my WTForms SelectField. import collections import wtforms STATE_ABBREV = ('AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IO', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT',

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

Multiple instances of the same form field

倖福魔咒の 提交于 2019-12-12 08:09:39
问题 I have invite form with two fields defined as person and email as follows: class InviteForm(Form): person = TextField("person", validators=[validators.Required("Please enter persons name.")]) email = EmailField("email", validators=[validators.Required("Please enter valid email."), validators.Email("Please enter valid email.")]) def validate(self): return validate_form(self) Where validate_form function is a cusotm validator which check few conditions for invite. My requirement is to allow

how to stack Vertically or Horizontally two MultiCheckboxField wtform fields

早过忘川 提交于 2019-12-12 05:29:11
问题 i have a form that uses a widget. what i want is two vertical columns side by side with the checkboxes. class MultiCheckboxField(SelectMultipleField): widget = widgets.ListWidget(prefix_label=False) option_widget = widgets.CheckboxInput() class SimpleForm2(Form): menu_items = MultiCheckboxField('Menu Item', choices=[], coerce=int) contents = MultiCheckboxField('Content', choices=[], coerce=int) submit = SubmitField('OK') for example Menu Item | Content cbox1 | cbox1' 回答1: This is a Horizontal

Wtfforms dynamic generation

陌路散爱 提交于 2019-12-12 05:25:10
问题 I have two wtfforms class SportStartForm(Form): ski = DateField(format='%d.%m.%Y') kitesurfing = DateField(format='%d.%m.%Y') windsurfing = DateField(format='%d.%m.%Y') surfing = DateField(format='%d.%m.%Y') class UpdateUserForm(Form): sport_start_at = FormField(SportStartForm) It works fine, but I want generate one of this form dynamically class SportStartForm(Form): def __new__(cls, **kwargs): for s in SPORTS: setattr(cls, s, DateField(format='%d.%m.%Y')) return super(SportStartForm, cls)._

How do I correctly submit results from a wtformusing Jinja with a Python /Flask setup

余生长醉 提交于 2019-12-12 01:36:09
问题 What I'm trying to do Instead of using form.predictions() I've tried to separate it out so I can style it better, although it doesn't work when I submit the predictions apart from the first prediction..i.e doesn't update it apart from the first one. I don't really want to write them all out hence the for loop and loop.index Issue Say if update one row of scores, it does not update the db, although I get a flash message saying I have. Strangely only row 1 works and the others dont. views #

Dynamic Flask-Form construction intended for use under Jinja

亡梦爱人 提交于 2019-12-11 23:27:17
问题 My purpose is to construct a form with dynamicly provided labels and use it in Jinja Form. This made me reveal multiple fundemental questions. As in the exemple here from flask_wtf import FlaskForm from wtforms import SubmitField from wtforms.validators import DataRequired class LoginForm(FlaskForm): # submit = SubmitField('Go On') def __init__(self, BtnLble): self.submit = SubmitField(BtnLble,form=self, name="MySbmt", _meta=self.Meta) # self.submit.bind(form=self, name="MySbmt", _meta=self

flaskform pass a variable (WTForms)

不羁岁月 提交于 2019-12-11 15:24:53
问题 I want to pass a str to be used as the prompt for a form. I thought it would be simple but it is proving to be difficult. Here is my code: class PostForm(FlaskForm): post = TextAreaField(Question, validators=[DataRequired()]) submit = SubmitField('Submit')` And, form = PostForm('my question') the corresponding html {{ wtf.quick_form(form) }} 回答1: So, I still don't have an answer to the question, but I did manage to come up with a solution. class PostForm(FlaskForm): post = TextAreaField(_l(