wtforms

WTForms getting the errors

自作多情 提交于 2019-12-20 12:31:04
问题 Currently in WTForms to access errors you have to loop through field errors like so: for error in form.username.errors: print error Since I'm building a rest application which uses no form views, I'm forced to check through all form fields in order to find where the error lies. Is there a way I could do something like: for fieldName, errorMessage in form.errors: ...do something 回答1: The actual form object has an errors attribute that contains the field names and their errors in a dictionary.

Flask-WTF form has errors during GET request

匆匆过客 提交于 2019-12-20 04:08:21
问题 I have a Flask view with a Flask-WTF form. When I load the page in the browser, the form always has errors, even though I haven't submitted it yet. Why does the form have errors before it is submitted? @app.route('/', methods=['GET', 'POST']) def index(): form = ApplicationForm(request.form) if form.is_submitted(): print "Form successfully submitted" if form.validate(): print "valid" print(form.errors) if form.validate_on_submit(): return redirect('index') return render_template('index.html',

MultipleFileField wtforms

僤鯓⒐⒋嵵緔 提交于 2019-12-20 03:34:08
问题 class AddProductForm(FlaskForm): product_pictures = MultipleFileField('Pictures') submit = SubmitField('Add Pictures') def product_add_pics(): form = AddProductForm() if form.validate_on_submit(): if form.product_pictures.data: for picture_upload in form.product_pictures.data: print(type(picture_upload)) form: <div class="form-group"> {{ form.product_pictures.label() }} {{ form.product_pictures(class="form-control-file") }} {% if form.product_pictures.errors %} {% for error in form.product

WTForms: IntegerField skips coercion when string value is '0'

风格不统一 提交于 2019-12-20 02:43:20
问题 This question almost covers what I am after here, but not quite. It seems like IntegerField skips coercion when the string value is '0'. Any other positive integer seems to work okay. Here is an example of '0': from wtforms import validators, Form, IntegerField from webob.multidict import MultiDict class TestForm(Form): num = IntegerField('How Many?', [validators.DataRequired('num required.'), validators.NumberRange(min=0, max=100)]) data_in = {'num': '0'} # Note '0' is a string as would be

Flask WTForms: Why is my POST request to upload a file not sending the file data?

ⅰ亾dé卋堺 提交于 2019-12-19 08:12:57
问题 I am trying to make a form to upload a file, but the file data is not being sent with the request. I'm manually navigating to my file and hitting submit. My FileRequired validator fails. (And if I don't include it the data field on form.scan_file is empty.) Here's my form: from flask_wtf import FlaskForm from flask_wtf.file import FileField, FileAllowed, FileRequired class ScanForm(FlaskForm): scan_file = FileField(validators=[FileAllowed(['nii', 'nii.gz', 'zip']), FileRequired()]) Here's my

WTForms creating a custom widget

我与影子孤独终老i 提交于 2019-12-19 05:58:23
问题 The WTForms documentation is woefully inadequate, they don't even show you one single example of a custom widget that isn't derived from another widget already. I am trying to make a button type, that isn't an <input> in html: submit = InlineButton(name='submit', type='submit', title='Save this page', textWithinSpan='Save') This is what I'm trying: from flask.ext.wtf import Required, Length, EqualTo, Field, TextInput, html_params from flask import Markup class InlineButtonWidget(object): text

I'm having problems with wtforms selectfields when i use a POST with Flask

穿精又带淫゛_ 提交于 2019-12-18 14:07:32
问题 I'm pretty new to wtforms and flask and was messing around with selectfields and got an error. The form itself works just fine without the selectfield but with it I get the following error: Error: ....fields.py", line 386, in pre_validate for v, _ in self.choices: TypeError: 'NoneType' object is not iterable I see the selectfield so it's being rendered. I suspect somehow the id is not being validated properly on POST and is returning none. Or it has something to do with my selectfield tuple

Dynamic Forms (Formsets) in Flask / WTForms?

与世无争的帅哥 提交于 2019-12-18 12:37:21
问题 In Django you have a multiple form feature called Formsets, which you can use to create multiple forms into the same template. I am trying to achieve something similar in Flask / WTforms. <form action="{{ url_for('request-accept') }}" method='post'> <table> <tbody> {% for request in requests %} <tr> <td> <div class="person-header"> <img src="{{request.profile_pic_url}}" class="img-circle profile-image"/> <p class="person-header-text">{{request.fullname()}}</p> </div> </td> <td> <input type=

WTForms-How to prepopulate a textarea field?

删除回忆录丶 提交于 2019-12-18 12:15:01
问题 Hi I have been trying to pepopulate a textareafield using something like this in the template. {{form.content(value="please type content")}} This works when the field is textfield primarily because the html accepts value for <input type="text"> but the same does not work for textarea... Can someone please help me with this? 回答1: For textarea widgets, you set the default content with the default argument in your field constructors. class YourForm(Form): your_text_area = TextAreaField("TextArea

Wtforms, add a class to a form dynamically

瘦欲@ 提交于 2019-12-18 11:06:42
问题 is there a way i could send a form's (css) class from python? For example: class Company(Form): companyName = TextField('Company Name', [validators.Length(min=3, max = 60)]) This renders a simple text field, but i want that text field to have the css class of .companyName , is that possible directly from python? I know that i can put a id="companyName" directly from python, but not class. Help. Update: I tried class_="companyName" and it did not work, i got: __init__() got an unexpected