wtforms

Flask WTForms: how do I get a form value back into Python?

自闭症网瘾萝莉.ら 提交于 2019-12-05 02:25:49
问题 What I'm trying to do is get user input on 5 different fields, then have what the user entered available to use in the rest of my program in python. The code I have so far, looks like this: class ArtistsForm(Form): artist1 = StringField('Artist 1', validators=[DataRequired()]) artist2 = StringField('Artist 2', validators=[DataRequired()]) artist3 = StringField('Artist 3', validators=[DataRequired()]) artist4 = StringField('Artist 4', validators=[DataRequired()]) artist5 = StringField('Artist

Pre-populating a BooleanField as checked (WTForms)

六月ゝ 毕业季﹏ 提交于 2019-12-05 02:15:35
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=q.question) }} {{ form.active(value=q.active) }} Show this question? If 'active' is True, I'd like the

Regex validation with WTForms and python

柔情痞子 提交于 2019-12-05 00:52:20
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 only alpha numeric characters. For some reason this regex is only checking the first character. Is

Tornado and WTForms

余生长醉 提交于 2019-12-04 22:29:50
问题 I am using WTForms for the first time. Using WTForms to validate POST requests in Tornado Below is my forms forms.py class UserForm(Form): user = TextField('user', [validators.Length(min=23, max=23)]) In the tonado handler I have def post(self): form = UserForm(self.request.body) The error message i get is: formdata should be a multidict-type wrapper that supports the 'getlist' method" How could I make this work? 回答1: wtforms-tornado 0.0.1 WTForms extensions for Tornado. pip install wtforms

Choices validation in WTForms does not update when database does

自闭症网瘾萝莉.ら 提交于 2019-12-04 08:28:30
I understand the SelectField method in WTForms takes can argument choices which has the form... choices=[("value1", "display of value 1"), ("value2", "display of value 2")] I need to populate my choices based on a call to the database. I'm using neo4j as my backend, so I can't use modelforms or the other built-in solutions for populating data in a form. def get_list_of_things(): query = 'start n = node:things("*:*") return ID(n), n.display_name;' t = cypher.execute(cdb, query)[0] for x in t: x[0] = str(x[0]) x[1] = str(x[1]) things = list(tuple(x) for x in t) return things class SelectAThing

can't append_entry FieldList in Flask-wtf more than one

天大地大妈咪最大 提交于 2019-12-04 07:57:24
I have form with flask-wtf for upload some image, also file field can be multiple my form: class ComposeForm(Form): attachment = FieldList(FileField(_('file')), _('attachment')) add_upload = SubmitField(_('Add upload')) my view: if form.validate_on_submit(): if form.add_upload.data: form.attachment.append_entry() return render_template('mailbox/compose.html', form=form) else: form.attachment.append_entry() my template : <form method="POST" enctype="multipart/form-data" action="."> {% for field in form %} {{field}} {% endfor %} </div> when i use enctype="multipart/form-data" in form append

How to override the html default “Please fill out this field” when validation fails in Flask?

偶尔善良 提交于 2019-12-04 07:23:47
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, put in your first name!')]) last_name = StringField('Last Name', validators=[DataRequired("What, you

Using WTForms' populate_obj( ) method with Flask micro framework

怎甘沉沦 提交于 2019-12-04 05:27:34
I have a template which allows the user to edit their user information. <form method="post"> <table> <tr> <td>Username:</td> <td>{{user['username']}}</td> </tr> <tr> <td>New Password:</td> <td> <input type="password" name="password"></td> <td>{% if form.password.errors %} {{form.password.errors}} {% endif %}<td> </tr> <tr> <td>Re-enter Password:</td> <td> <input type="password" name="confirm_password"> </td> </tr> <input type='hidden' name='username' value="{{user['username']}}"> <tr> <td><input type="submit" value="Submit"></td> </tr> </table> </form> I also have a view function for handling

WTForms error:TypeError: formdata should be a multidict-type wrapper

蹲街弑〆低调 提交于 2019-12-04 03:46:04
问题 from wtforms import Form, BooleanField, TextField, validators,PasswordField class LoginForm(Form): username = TextField('Username', [validators.Length(min=4, max=25)]) password = PasswordField('Password') when i use LoginForm on webapp(gae) like this : def post(self): form=LoginForm(self.request) but it show error : Traceback (most recent call last): File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 513, in __call__ handler.post(*groups) File "D:

rendering forms with flask + wtform

↘锁芯ラ 提交于 2019-12-04 03:20:42
问题 code in question: from flask import Blueprint, render_template, abort from flask.ext.wtf import Form import os from jinja2 import TemplateNotFound from models import Member from wtforms.ext.sqlalchemy.orm import model_form @simple_page.route('/register') def register(): form = model_form(Member, Form) return render_template('register.html', form=form, name="bad") class Member(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) email = db