wtforms

WTForms create variable number of fields

本秂侑毒 提交于 2019-12-03 12:45:19
问题 How I would dynamically create a few form fields with different questions, but the same answers? from wtforms import Form, RadioField from wtforms.validators import Required class VariableForm(Form): def __init__(formdata=None, obj=None, prefix='', **kwargs): super(VariableForm, self).__init__(formdata, obj, prefix, **kwargs) questions = kwargs['questions'] // How to to dynamically create three questions formatted as below? question = RadioField( # question ?, [Required()], choices = [('yes',

Python Flask WTForms: How can I disable a field dynamically in a view?

青春壹個敷衍的年華 提交于 2019-12-03 12:20:19
I've been able to implement this change to create Field which is disabled in WTForms. How would I selectively disable a field in my view before rendering it? fengling 姚 vim forms.py: add_time = DateTimeField( '添加时间', format='%Y-%m-%d %H:%M:%S', default=datetime.datetime.now(), # I use bs3,and it well add input an attribute disabled render_kw={'disabled':''}, validators=[DataRequired()], ) If you're trying to remove a field you could look at the Removing Fields Per-instance in the documentation. From the docs: Sometimes, you create a form which has fields that aren’t useful in all circumstances

WTforms form not submitting but outputs no validation errors

筅森魡賤 提交于 2019-12-03 09:01:28
I'm trying to get file uploads with flask-uploads working and running in to some snags. I'll show you my flask view function, the html and hopefully someone can point out what I'm missing. Basically what happens is that I submit the form and it fails the if request.method == 'POST' and form.validate(): check in the view function. It jumps down to display the template. wtforms isn't kicking me any errors on the form so I'm wondering why its failing that if statement. What am I over looking? Setting up flask-uploads: # Flask-Uploads photos = UploadSet('photos', IMAGES) configure_uploads(app,

Working with WTForms FieldList

爷,独闯天下 提交于 2019-12-03 08:17:41
问题 I use WTForms with Flask via the Flask.WTF extension. This question isn't Flask-specific, though. WTForms includes a FieldList field for lists of fields. I'd like to use this to make a form where users can add or remove items. This will require some sort of Ajax framework to dynamically add widgets, but the WTForms documentation makes no mention of it. Other frameworks like Deform come with Ajax support. Is there a similar framework available for WTForms? 回答1: I used something like this with

SelectField in wtforms and added <option> via javascript

*爱你&永不变心* 提交于 2019-12-03 07:30:11
I'm currently work on some project in pyramid and have problem with wtforms SelectField. I have a 3 SelectField fields: car_make (e.g., "audi") car_model (e.g., "audi 80") car_version (e.g., "AUDI 80 B4"). The car_make choices I can load in the view. The choices for rest of SelectFields (car_model, car_version) I will load on the client side via AJAX/javascript (I can choose car_model when car_make is selected and so on). The problem is that when I submit the form, car_model and car_version raise 'Not valid choice' because (in SelectField.pre_validation line 431) self.choices is empty. How can

wtforms Form class subclassing and field ordering

你说的曾经没有我的故事 提交于 2019-12-03 07:14:07
问题 I have a UserForm class: class UserForm(Form): first_name = TextField(u'First name', [validators.Required()]) last_name = TextField(u'Last name', [validators.Required()]) middle_name = TextField(u'Middle name', [validators.Required()]) username = TextField(u'Username', [validators.Required()]) password = TextField(u'Password', [validators.Required()], widget=PasswordInput()) email = TextField(u'Email', [validators.Optional(), validators.Email()]) and want to make the password field Optional

Disabled field is considered for validation in WTForms and Flask

佐手、 提交于 2019-12-03 05:11:24
问题 I have some fields in page disabled as for example:(using jinja2 templating system) <html> <body> <form action="" method=POST> {{ form.name(disabled=True) }} {{ form.title }} -- submit button -- </form> </body> </html> Field is disabled in the form as expected. In my views.py: On doing validate_on_submit() on form submit, it fails with validation error on 'name' field which is disabled. I was hoping that validation ignores disabled field. Is it the right behaviour? If so, can you please let

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

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)

WTForms create variable number of fields

僤鯓⒐⒋嵵緔 提交于 2019-12-03 02:22:06
How I would dynamically create a few form fields with different questions, but the same answers? from wtforms import Form, RadioField from wtforms.validators import Required class VariableForm(Form): def __init__(formdata=None, obj=None, prefix='', **kwargs): super(VariableForm, self).__init__(formdata, obj, prefix, **kwargs) questions = kwargs['questions'] // How to to dynamically create three questions formatted as below? question = RadioField( # question ?, [Required()], choices = [('yes', 'Yes'), ('no', 'No')], ) questions = ("Do you like peas?", "Do you like tea?", "Are you nice?") form =