flask-wtforms

Add a CSS class to an option in a WTForms SelectField

点点圈 提交于 2019-11-28 10:17:19
问题 Can anyone please tell me how to assign css class to choices values. I would like to change the background of each choices with small image, so how can I do it with wtforms and css? class RegisterForm(Form): username = TextField('username', [validators.Length(min=3, max=50), validators.Required()]) img_url = SelectField('avatar', choices=[('static/images/avatars/1.jpg', '1'), ('static/images/avatars/2.jpg', '2'), ('static/images/avatars/3.jpg', '3'), ('static/images/avatars/4.jpg', '4'), (

WTForms: two forms on the same page?

纵然是瞬间 提交于 2019-11-28 09:21:24
I have a dynamic web-page that should process two forms: a login form and a register form . I am using WTForms to process the two forms but I am having some trouble making it work, since both forms are being rendered to the same page. The following is the code for the login form of my webpage: PYTHON: class Login(Form): login_user = TextField('Username', [validators.Required()]) login_pass = PasswordField('Password', [validators.Required()]) @application.route('/index', methods=('GET', 'POST')) def index(): l_form = Login(request.form, prefix="login-form") if request.method == 'POST' and l

populate WTForms select field using value selected from previous field

余生长醉 提交于 2019-11-28 06:01:51
New to this, trying to build an app following a well known Flask tutorial, using Flask-bootstrap, Flask-wtforms, Jinja etc I have a form with 2 select fields and a button. class Form(FlaskForm): school_year = SelectField('School year', choices=some_tuples_list) category = SelectField('Category', choices=[]) submit = SubmitField('submit') I want only the first field to be pre-populated, and the other to get populated (on the client side?) based on the previous field's selected value. In the template I try something like {{ form.school_year(**{"onchange":"getCategories()"}) }} which works ok

Pre-Populate a WTforms in flask, with data from a SQLAlchemy object

一世执手 提交于 2019-11-28 05:27:33
I am fairly new to flask framework and was creating an edit profile page for a webportal. I am stuck at a point and am unable to autofill a form. Here is my form class : class EditProfile(Form): username = TextField('Username', [Required()]) email = TextField('Email', [Required()]) about = TextAreaField('About', [Required()]) website = TextField('Website', [Required()]) This is my function that evaluates the form. def editprofile(nickname = None): if g.fas_user['username'] == nickname or request.method == 'POST': form = EditProfile() form_action = url_for('profile.editprofile') if request

Validate WTForm form based on clicked button

…衆ロ難τιáo~ 提交于 2019-11-28 02:10:24
问题 On my form, I have two buttons that I use for submitting the form. One button deletes selected files (presented in a table, one checkbox to an object) and the other selects them for processing. When the files are selected on deletion, no validation is necessary (beyond checking that at least one file has been selected). However, for processing I need to make sure that there is exactly one file of a certain extension. Basically, I need different validation processes based on which button the

how to use QuerySelectField in flask?

核能气质少年 提交于 2019-11-27 23:07:22
I am trying to have a select field filled with the results of a sqlalchemy request in a flask form. Here are the code : def possible_book(): return Book.query.with_entities(Book.id).all() class AuthorForm(Form): familyname = TextField('familyname', [validators.Required()]) firstname = TextField('firstname', [validators.Required()]) book_list = QuerySelectField('book_list',query_factory=possible_book,get_label='title',allow_blank=True) This is the template : <form action="" method="post" name="edit_author" enctype="multipart/form-data"> {{form.hidden_tag()}} <p>Veuillez donner son prénom

AJax does not work with bootstrap-select

三世轮回 提交于 2019-11-27 21:24:36
I found flask-jquery-ajax example where the user selects an item from the vehicle "Make" drop down menu, the vehicle "Model" drop down menu is populated by making an AJAX request for a list of models for the make selected. I tried to replace the drop-down menus by bootstrap-select and as soon as I include class="selectpicker form-control" in the second drop-down menu it does not get any more populated after the first drop-down has been chosen. This is the HTML template: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Flask Jquery AJAX Drop Down Menu Example</title> <link

Dynamic choices WTForms Flask SelectField

可紊 提交于 2019-11-27 15:23:58
I'm trying to pass userID variable to WTForms with FlaskForms. First I'll show code that works fine and then what I need to modify(that the part I don't know how). I'm adding new Name associated with some group. FlaskForm model: class AddName(FlaskForm): name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)]) groupID = SelectField('Payload Type', choices=[(1,"Group1"),(2,"Group2")], validators=[InputRequired]) View model: @app.route('/dashboard/addname', methods=['GET', 'POST']) def addname(): form=AddName() if form.validate_on_submit(): name=Name(form.name.data

Dynamic select field using WTForms not updating

此生再无相见时 提交于 2019-11-27 14:15:52
问题 I'm trying to make a dynamic select field using wtforms and sqlalchemy, but it doesn't update when an item is inserted or deleted from the database. Here's my code: class UserForm(Form): username = StringField('Username', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()]) job = SelectField( 'Job', validators=[DataRequired()], choices=[(a.id, a.name) for a in Job.query.order_by(Job.name)] ) And the database model: class Job(db.Model): id = db.Column

Python Flask SQLAlchemy Pagination

邮差的信 提交于 2019-11-27 13:09:52
问题 I am having trouble implementing pagination with Flask-SQLAlchemy or Flask-Pagination, either or. I am unsure how to initialize the pagination, setting pages, determining pages, offest, etc. I am coming from PHP, quite new to Python. I am querying all the posts in my database posts = Posts.query.order_by(Posts.time.desc()).all() I have been looking at the following examples: http://www.ergo.io/tutorials/better-pagination-in-flask/better-pagination-in-flask/ https://pythonhosted.org/Flask