flask-wtforms

wtforms raise a validation error after the form is validated

送分小仙女□ 提交于 2019-12-10 02:56:35
问题 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 =

How to make a RadioField in Flask?

泄露秘密 提交于 2019-12-09 09:53:41
问题 I have a form with a TextField, FileField, and I want to add a RadioField. I'd like to have a radio field with two options, where the user can only select one. I'm following the example of the two previous forms that work. My forms.py looks like this from flask import Flask, request from werkzeug import secure_filename from flask.ext.wtf import Form, TextField, BooleanField, FileField, file_required, RadioField from flask.ext.wtf import Required class ImageForm(Form): name = TextField('name',

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

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

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

Bad request error when chosing file on server with form

孤者浪人 提交于 2019-12-08 13:03:26
问题 from flask.ext.wtf import Form from flask import send_from_directory from wtforms import StringField, BooleanField from wtforms import SelectField import os from os import listdir from os.path import isfile, join crewPath = ("/myproject/app/static/Crews") filenamesCrew = [f for f in listdir(crewPath) if isfile(join(crewPath,f)) ] class userInput(Form): json_fileCrew = SelectField(u"json_fileCrew", choices=[(f, f) for f in filenamesCrew]) def get_data(self): json = send_from_directory

Python: Couldn't dynamically set attributes to a FlaskForm?

我们两清 提交于 2019-12-08 11:51:57
问题 I want to set attributes dynamically to a FlaskForm like this form = ModelForm(request.form) file form.py class ModelForm(FlaskForm): def __init__(self, postData): super(ModelForm, self).__init__() for p in postData: setattr(ModelForm, p, StringField(p, validators=[InputRequired()])) But it only work for the second time running, the first time running, it doesn't work. I really don't understand how python constructor works. As this post, it said because class A is not fully initialized when

Pass variable from view to form

女生的网名这么多〃 提交于 2019-12-08 08:21:14
问题 @app.route('/product/<unique_form>', methods=['GET', 'POST']) def product(unique_form): form = ProductForm(**product_data) class ProductForm(Form): #form def __init__(self, *args, **kwargs): #Here in the __init__ I need to access the unique_form value Form.__init__(self, *args, **kwargs) I know that i can use sessions for this, but probably there is a way of pass the variable from the view to the form class. Something like this: form = ProductForm(unique_form, **product_data) This is possible

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

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

拈花ヽ惹草 提交于 2019-12-07 16:10:31
问题 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 + "