wtforms

Why use WTForms instead of just posting with HTML

限于喜欢 提交于 2019-12-01 07:32:52
I've been curious lately what the benefits of using WTForms to submit data to flask is? Plain HTML, JavaScript, or bootstrap form seem easier to style and easier to submit with. Plus you can leave out all the python code required to create a WTForms class. Can someone explain what an advantage would be? I could say CSRF protection is one of the greatest reasons, but there are a lot of reasons why one would use WTFroms over plain HTML forms. CSRF protection out of the box Form validation out the box WTForms come as classes, so all the good come's from an object form. basically, if your project

Get data from WTForms form

放肆的年华 提交于 2019-12-01 05:47:26
问题 How do I get the data from a WTForms form after submitting it? I want to get the email entered in the form. class ApplicationForm(Form): email = StringField() @app.route('/', methods=['GET', 'POST']) def index(): form = ApplicationForm() if form.validate_on_submit(): return redirect('index') return render_template('index.html', form=form) <form enctype="multipart/form-data" method="post"> {{ form.csrf_token }} {{ form.email }} <input type=submit> </form> 回答1: Each field has a data attribute

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

 ̄綄美尐妖づ 提交于 2019-12-01 05:36:00
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 views.py : from flask import Blueprint, render_template, request, flash, redirect, url_for, session

Why use WTForms instead of just posting with HTML

北慕城南 提交于 2019-12-01 05:27:22
问题 I've been curious lately what the benefits of using WTForms to submit data to flask is? Plain HTML, JavaScript, or bootstrap form seem easier to style and easier to submit with. Plus you can leave out all the python code required to create a WTForms class. Can someone explain what an advantage would be? 回答1: I could say CSRF protection is one of the greatest reasons, but there are a lot of reasons why one would use WTFroms over plain HTML forms. CSRF protection out of the box Form validation

WTForms creating a custom widget

*爱你&永不变心* 提交于 2019-12-01 04:07:11
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 = '' html_params = staticmethod(html_params) def __init__(self, input_type='submit', **kwargs): self

Type error on calling validate in wtforms

廉价感情. 提交于 2019-12-01 02:27:28
问题 I have TypeError in line where I call 'validate()' on my form. The error is: Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc

SQLAlchemy/WTForms: set default selected value for QuerySelectField

橙三吉。 提交于 2019-12-01 00:28:12
This [example][1] to set up a form with WTForms and SQLAlchemy in Flask and add a QuerySelectField to the form works. I am not using flask.ext.sqlalchemy , my code: ContentForm = model_form(Content, base_class=Form) ContentForm.author = QuerySelectField('Author', get_label="name") myform = ContentForm(request.form, content) myform.author.query = query_get_all(Authors) Now I want to set the default value of the QuerySelectField's selectlist . Tried passing a default kwarg in QuerySelectField and setting selected attributes. Nothing worked. Am I missing something obvious? Can someone help? You

SQLAlchemy/WTForms: set default selected value for QuerySelectField

我们两清 提交于 2019-11-30 18:29:04
问题 This [example][1] to set up a form with WTForms and SQLAlchemy in Flask and add a QuerySelectField to the form works. I am not using flask.ext.sqlalchemy , my code: ContentForm = model_form(Content, base_class=Form) ContentForm.author = QuerySelectField('Author', get_label="name") myform = ContentForm(request.form, content) myform.author.query = query_get_all(Authors) Now I want to set the default value of the QuerySelectField's selectlist . Tried passing a default kwarg in QuerySelectField

How do I validate wtforms fields against one another?

做~自己de王妃 提交于 2019-11-30 17:19:41
I have three identical SelectField inputs in a form, each with the same set of options. I can't use one multiple select. I want to make sure that the user selects three different choices for these three fields. In custom validation, it appears that you can only reference one field at a time, not compare the value of this field to others. How can I do that? Thanks! You can override validate in your Form ... class MyForm(Form): select1 = SelectField('Select 1', ...) select2 = SelectField('Select 2', ...) select3 = SelectField('Select 3', ...) def validate(self): if not Form.validate(self):

WTForms: How to select options in SelectMultipleField?

蹲街弑〆低调 提交于 2019-11-30 12:05:23
问题 Choices can be set using form.myfield.choices=[("1","Choice1"), ("2","Choice2")] What is the way to set the selected option? 回答1: You can use the choices and default keyword arguments when creating the field, like this: my_choices = [('1', 'Choice1'), ('2', 'Choice2'), ('3', 'Choice3')] SelectMultipleField(choices = my_choices, default = ['1', '3']) This will mark choices 1 and 3 as selected. Edit: Default values are apparently processed (copied into the data member) when the form is