flask-wtforms

Flask-wtf: csrf_token is removed from session before I can POST my form

流过昼夜 提交于 2019-12-05 19:49:55
I'm using Flask with Flask-Security (specifically Flask-WTF regarding my csrf issue) to "ease" the process of register/loggin users (not easy so far). I'm using BackboneJS on the front-end, therefore I kind of hacked the original way to use Flask-WTF. Indeed, I make an AJAX GET request on /register to get the register page (generated by Flask-Security) and I put the resulting HTML in a modal. render: function () { var self = this; $.ajax({ type: 'GET', url: Config.constants.serverGateway + "/register" }).done(function(result){ console.log("get register done", result); var html = self.template(

wtforms raise a validation error after the form is validated

痞子三分冷 提交于 2019-12-05 03:31:29
I have a registration form that collects credit card info. The workflow is as follows: User enters registration data and card data through stripe. The form is validated for registration data. If the form is valid, payment is processed. If payment goes through, it's all good, the user is registered and moves on. If the payment fails, i want to be able to raise a validation error on a hidden field of the form. Is that possible? Here's a the form submission code: def register(): form = RegistrationForm() if form.validate_on_submit(): user = User( [...] ) db.session.add(user) #Charge amount =

flask wtforms selectfield choices not update

偶尔善良 提交于 2019-12-04 20:45:15
class ArticleForm(Form): type = SelectField('type', choices=[(h.id, h.name) for h in ArticleType.query.all()], coerce=int) below is how I use the ArticleForm in views @admin.route('/article/add',methods=['get','post']) def article_create(): article_form = ArticleForm() my problem is that the selectField is not read the db each time I visit /article/add If I add a new type in the ArticleType the choice of the selectField will not update the choice until I restart the server. but If I use like below @admin.route('/article/add',methods=['get','post']) def article_create(): article_form =

python wtf AttributeError: 'ObjectIdField' object has no attribute 'help_text'

柔情痞子 提交于 2019-12-04 19:17:07
Based on this tutorial I am trying to create a form to get a few measurements. It seems that the part to display the data is working but when using the model_form command to generate the input form it breaks with this error: File "/myproject/lib/python3.4/site-packages/flask_mongoengine/wtf/orm.py", line 49, in convert 'description': field.help_text or '', AttributeError: 'ObjectIdField' object has no attribute 'help_text' The error happens on this line of my code: form_cls = model_form(Measurement, exclude=('id', 'created_at', 'comments')) This is my view.py code: from flask import Blueprint,

Pass a variable to a Flask WTForm

南楼画角 提交于 2019-12-04 19:02:27
I want to do a query select field with a default value that is passed in from the route. I can't figure out how to pass a variable from the View to the Form class class transactionsForm(Form): loan_id = QuerySelectField('trans_id', validators=[Required()], get_label='name', query_factory=lambda: trans.query.filter_by(trans_id=[MY VARIABLE]).all()) Miguel This is from the QuerySelectField documentation: The query property on the field can be set from within a view to assign a query per-instance to the field. If the property is not set, the query_factory callable passed to the field constructor

Flask self.errors.append() - AttributeError: 'tuple' object has no attribute 'append'

Deadly 提交于 2019-12-04 10:44:38
My small registration app gives and error when I try to validate the submited data by user and check if the entered e-mail exists. here is my files: forms: from flask.ext.wtf import Form from wtforms import TextField, BooleanField, PasswordField, TextAreaField, validators from wtforms.validators import Required from app import models class RegisterForm(Form): """RegisterForm class needed for retrieving data from user""" username = TextField('username', [validators.Length(min=3, max=50), validators.Required()]) email = TextField('email', [validators.Length(min=3, max=100), validators.Required()

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

Flask WTF 'StringField' object has no attribute 'translate'

痞子三分冷 提交于 2019-12-04 03:34:52
问题 I am relatively new to Python, and I've been following the Miguel Grinberg Flask Mega-Tutorial. I have a very simple form which, when I try to submit, I receive the following error: AttributeError: 'StringField' object has no attribute 'translate' Here is the form: from flask.ext.wtf import Form from wtforms import StringField from wtforms.validators import DataRequired class CreateSubjectForm(Form): name = StringField('name', validators=[DataRequired()]) And views.py : @app.route('/create

How do I use “tel”, “number”, or other input types in WTForms?

喜欢而已 提交于 2019-12-04 01:30:36
问题 I want to use a phone number field in my form. What I need is when this field is tapped on Android phone, not general keyboard, but digital only appears. I learned that this can be achieved by using <input type="tel" or <input type="number" . How do I use the tel or number input types in WTForms? 回答1: This appears to be missing from the WTForms docs, but there are field definitions for all the input types added in HTML 5. from wtforms.fields.html5 import TelField phonenumber = TelField() 回答2:

How can I dynamically add a WTForms TextField to a FieldList using jQuery?

孤人 提交于 2019-12-03 20:44:39
I want to add or remove new WTForm input fields with button, using Jquery, just like here http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery/comment-page-1 but using my form-fields. my form: class EditBook(Form): title = TextField('title', validators = [Required()]) authors = FieldList(TextField()) problem is that I can't just append for example $(InputsWrapper).append("{{form.authors(size=20)}}"); it just prints this text. This answer is based on nsfyn55's explanations (first paragraph). I had a similar problem. The solution was to use: https://github.com/Rhyzz