flask-wtforms

Multiple instances of the same form field

浪子不回头ぞ 提交于 2019-12-03 15:22:47
I have invite form with two fields defined as person and email as follows: class InviteForm(Form): person = TextField("person", validators=[validators.Required("Please enter persons name.")]) email = EmailField("email", validators=[validators.Required("Please enter valid email."), validators.Email("Please enter valid email.")]) def validate(self): return validate_form(self) Where validate_form function is a cusotm validator which check few conditions for invite. My requirement is to allow users to invite more than one person at a time. To achieve this I have added jquery function which

Generate a dynamic form using flask-wtf and sqlalchemy

浪尽此生 提交于 2019-12-03 13:38:22
问题 I have a webapp that allows users to create their own fields to be rendered in a form later on. I have a model Formfield like so: class Formfield(db.Model): id = db.Column(db.Integer, primary_key = True) form_id = db.Column(db.Integer, db.ForeignKey('formbooking.id')) label = db.Column(db.String(80)) placeholder_text = db.Column(db.String(80)) help_text = db.Column(db.String(500)) box_checked = db.Column(db.Boolean, nullable = True, default = False) options = db.Column(db.String) # JSON goes

How to make a RadioField in Flask?

血红的双手。 提交于 2019-12-03 12:20:25
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', validators = [Required()]) fileName = FileField('fileName', validators=[file_required()])

How to create chained selectfield in flask without refreshing the page?

旧街凉风 提交于 2019-12-03 11:21:30
问题 I am currently working on an address form using wtf, which contains Country, State, City..etc. The database is all set with FK. class Country(db.Model): __tablename__ = 'countries' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) users = db.relationship('User', backref='countries', lazy='dynamic') class City(db.Model): __tablename__ = 'cities' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) countries_id = db

Flask-Restful POST fails due CSRF protection of Flask-WTF

帅比萌擦擦* 提交于 2019-12-03 09:43:07
问题 I am using normal flask web + flask-restful. So I need CSRF protection for web but not for REST. The moment I enable CsrfProtect(app) of flask-wtf , all my post unit tests for flask-restful return a 400. Is there a way to disable CSRF protection for REST services since they are coming from mobile handsets without session handling anyway, hence CSRF wouldn't make much sense. This is how I test it: rv = self.client.post('api/v1.0/verify-email', environ_base={'REMOTE_ADDR': '127.0.0.1'}, headers

Creating a form with a varying number of repeated subform in Flask/WTForms

自作多情 提交于 2019-12-03 07:46:25
问题 My model currently has three related objects (there are more, but only three are relevant to this problem). User, Network, and Email. What I want to be able to do is have a defined set of Networks, and to allow each User to have an Email address on each Network (these are slightly more complex, but I've cut them down to what I think is relevant). class User(UserMixin, db.Model): """ The User object. """ __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) # email = db.Column

Python Flask-WTF - use same form template for add and edit operations

半腔热情 提交于 2019-12-03 05:59:40
问题 I'm just getting started with Flask / Flask-WTF / SQLAlchemy, and most example CRUD code I see shows separate templates for adding / editing. It seems repetitive to have two templates with almost identical form html (e.g. books_add.html, books_edit.html). Conceptually it makes more sense to me to have one template, something like "books_form.html", and just call render_template on that same template from two separate route definitions. I'm not quite sure how to accomplish it though, something

Flask-SQLAlchemy: How to conditionally insert or update a row

感情迁移 提交于 2019-12-03 04:22:43
问题 My application uses a combination of Flask, Flask-SQLAlchemy, Flask-WTF and Jinja2. In its current incarnation, I have a settings table. The table will only have one record with one field. Initially the table contains zero records. What I want to achieve is: Given that no entries exist in db, then show empty form ready for user input Given that an entry exist, show the entry, and if the user changes the value, then update the rec in db. Here is my code: models.py class Provider(db.Model): id

How to use Flask-WTForms CSRF protection with AJAX?

自作多情 提交于 2019-12-03 03:27:28
Flask-WTForms provides CSRF protection. It works great when using normal HTML forms, but the process is less clear when using AJAX. I have a file upload in my form, and I split the process in two with AJAX: the file goes to the upload endpoint while the rest of the form goes to the submit endpoint. Since the file is posted with AJAX, it doesn't get a CSRF token, but I want to protect the upload endpoint from attacks. How can I generate a CSRF token when using AJAX? @app.route('/submit', methods=["GET","POST"]) @login_required def submit(): form = MyForm() if request.method == "POST" and form

Generate a dynamic form using flask-wtf and sqlalchemy

走远了吗. 提交于 2019-12-03 03:04:29
I have a webapp that allows users to create their own fields to be rendered in a form later on. I have a model Formfield like so: class Formfield(db.Model): id = db.Column(db.Integer, primary_key = True) form_id = db.Column(db.Integer, db.ForeignKey('formbooking.id')) label = db.Column(db.String(80)) placeholder_text = db.Column(db.String(80)) help_text = db.Column(db.String(500)) box_checked = db.Column(db.Boolean, nullable = True, default = False) options = db.Column(db.String) # JSON goes here? answer = db.Column(db.String) required = db.Column(db.Boolean, nullable = False, default = False)