flask-wtforms

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

喜夏-厌秋 提交于 2019-12-03 00:50:46
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.Column(db.Integer, db.ForeignKey('countries.id')) Now I am trying to achieve a chained selectfield sort

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

旧城冷巷雨未停 提交于 2019-12-02 21:14:28
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(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True)

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

吃可爱长大的小学妹 提交于 2019-12-02 18:31:24
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 like: @app.route('/books/add') def add_book(): ... render_template('books_form.html', action = 'add')

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

强颜欢笑 提交于 2019-12-02 17:37:30
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 = db.Column(db.Integer, primary_key = True) rssfeed = db.Column(db.String(120), unique = True) def _

TypeError: __init__() got an unexpected keyword argument 'password'

安稳与你 提交于 2019-12-02 08:46:27
问题 I can't find out what's the cause of this error TypeError: __init__() takes at most 2 arguments (3 given) which is pointing to @user_blueprint.route('/login', methods=['GET', 'POST']) def login(): if g.user.is_authenticated: flash("You are already Logged in", 'warning') return redirect(url_for('members')) error = "" form = LoginForm() if request.method == 'POST': if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user is not None and bcrypt.check

WTForms: IntegerField skips coercion on a string value

南楼画角 提交于 2019-12-02 08:08:43
问题 I have a Form instance with a single IntegerField . The IntegerField renders to HTML as an <input> with type="text" and data gets POSTed back from an HTML form as a text string. However the form will not validate if the posted data has a string value for the IntegerField (passed in via a dict in the data parameter). Here's a toy example: from wtforms import validators, Form, IntegerField class TestForm(Form): num = IntegerField('How Many?', [validators.NumberRange(min=1, max=100)]) test_form1

Flask WTForms always give false on validate_on_submit()

随声附和 提交于 2019-12-02 06:30:50
问题 I have created a signup form using wtforms. I am using FormField in it so that I don't have to repeat some of the elements of the form again. But whenever I click on the Submit button it always give me false on validate_on_submit method invocation. Not getting why is this happening. My form.py is as follows: class ProfileInfoForm(Form): firstname = TextField('firstname', validators= [validators.Required("Please enter First name.")]) lastname = TextField('lastname', validators= [validators

sqlalchemy.exc.InterfaceError: <unprintable InterfaceError object>

让人想犯罪 __ 提交于 2019-12-02 04:04:41
问题 I'm trying out Flask but I'm having the error sqlalchemy.exc.InterfaceError: <unprintable InterfaceError object> while submitting a wtforms. The model class is: class Post(db.Model): __tablename__ = 'blog_posts' id = db.Column(db.Integer, unique=True, primary_key=True) title = db.Column(db.String(50), unique=False) content = db.Column(db.Text, unique=False) user_id = db.Column(db.String, db.ForeignKey('users.username')) @staticmethod def post_new_entry(title, content, user_id): """ Post new

Flask-WTF form has errors during GET request

耗尽温柔 提交于 2019-12-02 02:59:48
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', form=form) 127.0.0.1 - - [30/Nov/2016 16:54:12] "GET / HTTP/1.1" 200 - {'department': [u'Not a valid

sqlalchemy.exc.InterfaceError: <unprintable InterfaceError object>

≡放荡痞女 提交于 2019-12-02 01:33:15
I'm trying out Flask but I'm having the error sqlalchemy.exc.InterfaceError: <unprintable InterfaceError object> while submitting a wtforms. The model class is: class Post(db.Model): __tablename__ = 'blog_posts' id = db.Column(db.Integer, unique=True, primary_key=True) title = db.Column(db.String(50), unique=False) content = db.Column(db.Text, unique=False) user_id = db.Column(db.String, db.ForeignKey('users.username')) @staticmethod def post_new_entry(title, content, user_id): """ Post new entry to database """ new_post = Post(title=title, content=content, user_id=user_id) db.session.add(new