wtforms

Flask - wtforms: Validation always false

空扰寡人 提交于 2019-12-09 08:49:18
问题 First, I'm new to python and Flask, so I'm sorry if my question is dumb. I search it but never found an answer (which should be an "easy" one I guess). I wanted to add a contact page in my website, I found this tutorial so I followed it. Everything worked fine until the forms validation. I only use Required and form.validate() always returned false. If I don't touch my code, and I remove every Required in the form class, it works fine, form.validate() returns true. I don't really understand

Flask - WTForm - save form to db

◇◆丶佛笑我妖孽 提交于 2019-12-09 01:29:07
问题 I have problem with saving form (wtf) to (sqlalchemy) db ,form is rendering ,but after submit nothing happens ... e.g. : 127.0.0.1 - - [30/Dec/2013 10:30:24] "POST /add/ HTTP/1.1" 200 - I was trying to save without validators e.g. : name = TextField('Task Name') and i was trying to save form other way : if request.method == 'POST' and form.validate(): new_task = Tasks( form.name.data, form.due_date.data, form.priority.data, form.posted_date.data, session['user_id'], form.category.data, form

How to auto-generate full HTML from a WTForms form

佐手、 提交于 2019-12-08 19:15:50
问题 I'm trying to create a simple WTForms-based admin interface for an SQLAlchemy app, using Jinja2 templates. I've read the docs of WTForms-Alchemy and I understand that it can auto-generate a form from my model just via a few lines of code, like: class UserForm(ModelForm): class Meta: model = User My problem is that even though I have this form auto-generated, I found no resource anywhere about how can I make it into a functional HTML page. There are a few snippets about rendering errors for

Get None from a Fields data in instead of an empty string

邮差的信 提交于 2019-12-08 18:16:00
问题 I have this field in the WTForms form name = StringField('Name', validators = [Optional(), Length(max = 100)]) When the field is submitted empty then form.name.data will, as expected, contain an empty string. Is there some way to make it return None in instead of an empty string? It is just because it is very convenient to deal with null in the database like in this update : update t set name = coalesce(%(name)s, name), other = coalesce(%(other)s, other) With the above code I don't need to

Dynamic SelectField validation fails with: “Not a valid choice”

亡梦爱人 提交于 2019-12-08 14:17:31
Here's my code, i can't get past the "not a valid choice" on the SelectField, being it in the creation form or the editing one... The categories i'm passing it as choices are unicode, even so i tried various "coerce" settings in the Form SelectField class ProductsView(MyModelView): create_template = '/admin/edit-products.html' form = ProductForm def create_form(self, model=None): form = self.form() choices = list(db.db.categories.find()) choices.sort(key=lambda x: x['order']) sorted_choices = [(str(cat['name']), cat['name']) for cat in choices] print sorted_choices form.category.choices =

How to evaluate this condition in HTML jinja template based on id

僤鯓⒐⒋嵵緔 提交于 2019-12-08 13:50:31
问题 Assume i have <tr> <th>Action</th> <td id="action_id"></td> </tr> Now i want to write a condition where i display something based on the value of action_id, i.e {% if action_id == "on" %} display {%else%} I am trying to display this , this is related to How to populate a submit popup based on the value you filled to evaluate a condition | flask 回答1: Jinja2 Template Engine has access to only those variables which are passed to it via the view function, and since the variable which you are

Submit WTform with dynamically generated fields

倾然丶 夕夏残阳落幕 提交于 2019-12-08 05:40:46
问题 I have a form where users can dynamically add fields to it. While submitting this form, backend only sees the fields which backend generated #forms.py class ExpensesForm(FlaskForm): expense_name = StringField('Expense_Item', validators=[DataRequired()]) cost = FloatField('Cost', validators=[DataRequired()]) due_date = DateField('Due Date', format='%Y-%m-%d', validators=[DataRequired()], default=datetime.datetime.today().date()) type = SelectField('Role', choices=[('mutual', 'Mutual'), (

Dynamic SelectField validation fails with: “Not a valid choice”

人走茶凉 提交于 2019-12-08 04:11:20
问题 Here's my code, i can't get past the "not a valid choice" on the SelectField, being it in the creation form or the editing one... The categories i'm passing it as choices are unicode, even so i tried various "coerce" settings in the Form SelectField class ProductsView(MyModelView): create_template = '/admin/edit-products.html' form = ProductForm def create_form(self, model=None): form = self.form() choices = list(db.db.categories.find()) choices.sort(key=lambda x: x['order']) sorted_choices =

WTForms: Passing extra arguments while writting custom validation

一曲冷凌霜 提交于 2019-12-08 03:30:21
问题 While writing custom validation for wtforms, is it possible to pass extra argument( like request) For e.g class MyForm(Form): name = TextField('Name', [Required()]) def validate_name(form, field): if len(field.data) > 50: raise ValidationError('Name must be less than 50 characters') I need to pass request object( or non form object), if possible to validate_name method. Is there any way for doing it? 回答1: The easier way to proceed would be to pass the request object to your form and store it

upload file in ajax with wtforms

 ̄綄美尐妖づ 提交于 2019-12-08 02:25:30
问题 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