flask-wtforms

Filling WTForms FormField FieldList with data results in HTML in fields

回眸只為那壹抹淺笑 提交于 2019-11-27 10:23:18
问题 I have a Flask app in which I can populate form data by uploading a CSV file which is then read. I want to populate a FieldList with the data read from the CSV. However, when I try to populate the data, it enters raw HTML into the TextFields instead of just the value that I want. What am I doing wrong? app.py from flask import Flask, render_template, request, url_for from flask.ext.wtf import Form from wtforms import StringField, FieldList, FormField, SelectField from wtforms.validators

flask-wtforms field required

£可爱£侵袭症+ 提交于 2019-11-27 09:47:15
how i can add tag required on this flask code : {{ form.youtube_href(type='url', class='form-control') }} actual output is : <input class="form-control" id="youtube_href" name="youtube_href" value="" type="url"> need this output bat give error : <input class="form-control" id="youtube_href" name="youtube_href" value="" type="url" required> im tried this bat give error : {{ form.youtube_href(type='url', class='form-control', 'required') }} As of WTForms 2.2 (June 2nd, 2018), fields now render the required attribute if they have a validator that sets the required flag, such as DataRequired and

How to populate wtform select field using mongokit/pymongo?

萝らか妹 提交于 2019-11-27 07:34:23
问题 I'm trying to create a SelectField using a mongodb query, but so far I haven't been successful: # forms.py in blueprint CATEGORIES = [] for item in db.Terms.find(): CATEGORIES.append((item['slug'], item['name'])) class TermForm(Form): category = SelectField( choices=CATEGORIES, validators=[Optional()]) But I get an exception: Traceback (most recent call last): File "/home/one/Projects/proj/manage.py", line 14, in <module> app = create_app(os.getenv('FLASK_CONFIG') or 'default') File "/home

How do I validate wtforms fields against one another?

人走茶凉 提交于 2019-11-27 05:54:41
问题 I have three identical SelectField inputs in a form, each with the same set of options. I can't use one multiple select. I want to make sure that the user selects three different choices for these three fields. In custom validation, it appears that you can only reference one field at a time, not compare the value of this field to others. How can I do that? Thanks! 回答1: You can override validate in your Form ... class MyForm(Form): select1 = SelectField('Select 1', ...) select2 = SelectField(

WTForms: two forms on the same page?

こ雲淡風輕ζ 提交于 2019-11-27 05:50:56
问题 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'

Form is never valid with WTForms

不打扰是莪最后的温柔 提交于 2019-11-27 05:40:51
I have a Flask-WTF form for sign in. Apparently the form is never valid, no matter what I enter "success" is never printed. Why isn't my form validating? class loginForm(Form): email = EmailField('email', validators=[InputRequired("Please enter your email address."), Email("Please enter a valid email address.")]) password = PasswordField('password', validators=[InputRequired("Please enter your password.")]) @app.route('/sign-in', methods=['POST', 'GET']) def signIn(): form = loginForm(request.form) if form.validate_on_submit(): print 'success' return redirect('/') return render_template(

Clear valid form after it is submitted

前提是你 提交于 2019-11-27 05:18:32
I want to reset the form after it validates. Currently the form will still show the previous data after it is submitted and valid. Basically, I want the form to go back to the original state with all fields clean. What is the correct to do this? @mod.route('/', methods=['GET', 'POST']) def home(): form = NewRegistration() if form.validate_on_submit(): #save in db flash(gettext(u'Thanks for the registration.')) return render_template("users/registration.html", form=form) The issue is that you're always rendering the form with whatever data was passed in, even if that data validated and was

Form validation fails due missing CSRF

元气小坏坏 提交于 2019-11-27 04:59:55
A few days ago I have reset my local flask environment without having captured the dependencies via a pip freeze before I deleted it. Hence I had to re-install the latest version of the entire stack. Now out of the blue I am no longer able to validate with forms. Flask claims CSRF would be missing. def register(): form = RegisterForm() if form.validate_on_submit(): ... return make_response("register.html", form=form, error=form.errors) The first time I send a Get I retrieve an empty form.errors as expected. Now I fill out the form and submit it and form.errors is showing: {'csrf_token': [u

wtforms, CSRF, flask, FieldList

只谈情不闲聊 提交于 2019-11-27 03:51:42
问题 I'm having trouble passing through validation when using a FieldList with WTForms. I keep getting this error. {'csrf_token': [u'CSRF token missing']} . The problem is if I do not have any data to validate in the FieldList field, the validation passes and there are no issues. But when I try to validate the form with any data I get that error. Here are my forms: class FilterForm(wtf.Form): filter_value = wtf.TextField('Value', validators=[validators.Required()]) filter_operator = wtf

populate WTForms select field using value selected from previous field

荒凉一梦 提交于 2019-11-27 01:03:10
问题 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