wtforms

Get selected text from a form using wtforms SelectField

牧云@^-^@ 提交于 2019-12-29 07:43:12
问题 This is a question upon the use of wtforms SelectField . Once the form submitted, I wish to extract selected text. I have the following form: from wtforms import Form, SelectField class TestForm(Form): hour = SelectField(u'Hour', choices=[('1', '8am'), ('2', '10am') ]) Here's the view: @app.route('/', methods=['GET', 'POST']) def test_create(): form =TestForm(request.form) if request.method == 'POST' and form.validate(): test = Test() form.populate_obj(test) test.hour=form.hour.name db

How to make a field conditionally optional in WTForms?

雨燕双飞 提交于 2019-12-28 04:49:06
问题 My form validation is working nearly complete, I just have 2 cases I don't know exactly how to solve: 1) The password field should be required of course but I also provide the possibility to log in with google or facebook account via OAuth and then name gets prefilled but I remove the password field completely from the form is there is a user (google) or a facebook user object: <tr><td> <br /> {% if user or current_user %} {% else %} <div class="labelform"> {% filter capitalize %}{% trans %

Get data values from QuerySelectField with query_factory

大城市里の小女人 提交于 2019-12-25 08:37:22
问题 I have problem to save data from QuerySelectField with query_factory. I will always end with: Error message InterfaceError: (sqlite3.InterfaceError) Error binding parameter 2 - probably unsupported type. [SQL: u'INSERT INTO asset_objects (asset_name, asset_type, asset_owner) VALUES (?, ?, ?)'] [parameters: (u'd', u'Process', <__main__.Model_New_Users object at 0x03819A50>)] DB Model # --- New Information Assets --- class Model_New_Asset(db.Model): __tablename__ = 'asset_objects' id = db

Flask WTForms form does not validate

你。 提交于 2019-12-25 03:49:33
问题 I cannot get a certain simple Flask WTForm to validate. After a couple of days of struggle, I've tried everything I can think of. I'm new to Flask and Web programming in general. Here's a stripped-down but working version of my code. The only action of the test code (other than submitting the form) is to print messages to terminal. Its appearance when running: This is views.py: # -*- coding: utf_8 -*- ... @app.route('/test/new/', methods=['GET','POST']) def newTest(): form = TestForm(request

WTForms not validating NumberRange

会有一股神秘感。 提交于 2019-12-25 01:45:28
问题 I'm making a WTForm which takes Decimals as inputs, and I'm trying to restrict input to a range of numbers (between 0 and 10 inclusive). However, the validator NumberRange doesn't seem to do anything. Python (using flask): from flask import render_template from flask_wtf import FlaskForm from wtforms import DecimalField, SubmitField, validators class NumberForm(FlaskForm): question = DecimalField('Question 1', [validators.NumberRange(min=0, max=10, message="blah"), validators.Optional()])

Raising an error in WTForm using jinja2

时光毁灭记忆、已成空白 提交于 2019-12-24 10:12:19
问题 I'm trying to raise an error in Jinja2, in a WTForm, the error should be raised if url input is not validated, but when i submit an invalide url, i get a popup saying "Please enter a url". how do i pass the default popup and add a custom error message ? here is the main py: from datetime import datetime from flask import Flask, render_template, url_for, request, redirect,flash from logging import DEBUG from flask_wtf import FlaskForm from wtforms import StringField, PasswordField from flask

Python Flask WTForm SelectField with Enum values 'Not a valid choice' upon validation

♀尐吖头ヾ 提交于 2019-12-24 09:03:54
问题 My Python Flask app is using WTForms with built in python Enum support. I'm attempting to submit a form (POST) where a SelectField is populated by all values of a Enum. When I hit 'Submit' i'm given the error, 'Not a valid choice.' This seems strange because when checking the values of the incoming form, the form seemingly does contain a valid choice from the list of Enum values provided. I'm using a subclass of Enum named AJBEnum which is formatted like so: class UserRole(AJBEnum): admin = 0

How to get QuerySelectField current value with Flask WTF

て烟熏妆下的殇ゞ 提交于 2019-12-24 08:07:01
问题 If using SelectField we can get the current value that prepopulate in form from database using the obj argument, like this answer on my previous question. For now, I want to get the current value using QuerySelectField . Here is the snippet of my code: def course_list(): return Course.query.all() class PaymentForm(Form): total_price = IntegerField(validators=[required()]) course_name = QuerySelectField('Course name', validators=[required()], query_factory=course_list) # .... # .... And here

Flask wtforms - 'UnboundField' object is not callable, dynamic field won't init properly

时光怂恿深爱的人放手 提交于 2019-12-24 03:08:24
问题 app.py from flask import Flask, render_template from flask_wtf import FlaskForm from wtforms import StringField, SubmitField, FieldList, FormField app = Flask(__name__) app.config['SECRET_KEY'] = 'apple pie' class BookForm(FlaskForm): book = StringField('book title') class LibraryForm(FlaskForm): def __init__(self, min_entries=0, *args, **kwargs): super(LibraryForm, self).__init__(*args, **kwargs) self.books = FieldList(FormField(BookForm), min_entries=min_entries) library = StringField(

Flask, WTForms: Is there a way to make a StringField in the form _temporarily_ hidden?

戏子无情 提交于 2019-12-23 23:17:51
问题 This is my model: class F(Form): a = StringField('a', validators = [validators.DataRequired()]) Is there a way to make a StringField in the form temporarily hidden? Something like: @app.route('/f', methods = ['GET', 'POST']) def f(): form = F(request.form) if foo(form): form.a.__MakeTemporarilyHidden__() else: form.a.__MakeItVisibleAgain__() if request.method == 'GET': return render_template('f.html', form = form) I am aware of wtforms.fields.HiddenField but I want to dynamically switch