wtforms

wtforms, generate fields in constructor

≡放荡痞女 提交于 2019-12-11 03:10:00
问题 I need to generate my fields in the constructor of the form since the number of fields needed can vary. I think my current solution is the problem. I get an exception when I try to expand the form in my template saying AttributeError: 'UnboundField' object has no attribute ' call ' What is wrong with this code? class DriverTemplateSchedueForm(Form): def __init__(self, per_day=30, **kwargs): self.per_day = per_day ages = model.Agency.query.all() ages = [(a.id, a.name) for a in ages] self.days

Using WTForms with Enum

天大地大妈咪最大 提交于 2019-12-10 19:33:38
问题 I have the following code: class Company(enum.Enum): EnterMedia = 'EnterMedia' WhalesMedia = 'WhalesMedia' @classmethod def choices(cls): return [(choice.name, choice.name) for choice in cls] @classmethod def coerce(cls, item): print "Coerce", item, type(item) if item == 'WhalesMedia': return Company.WhalesMedia elif item == 'EnterMedia': return Company.EnterMedia else: raise ValueError And this is my wtform field: company = SelectField("Company", choices=Company.choices(), coerce=Company

Create a custom field in wtForms

会有一股神秘感。 提交于 2019-12-10 13:37:07
问题 In my form I am trying to create a custom array field with choices. The custom form field: class CustomField(Field): widget = TextInput() def _value(self): if self.data: return u', '.join(self.data) else: return u'' def process_formdata(self, valuelist): if valuelist: self.data = [x.strip() for x in valuelist[0].split(',')] else: self.data = [] The actual form calls the custom form field class PostForm(Form): status = CustomField() Whenever, I post data to PostForm it calls the custom field

flask-wtf selectField choices not valid

痞子三分冷 提交于 2019-12-10 11:18:47
问题 I made a SelectField like this: # constants.py QUESTION_LIST = {} QUESTION_LIST['QuestionOne'] = { 'disagree-strong': "Strongly Disagree", 'agree-strong': "Strongly Agree" } #forms.py from constants import * typeone = SelectField('QuestionOne', choices=QUESTION_LIST['QuestionOne'].iteritems(), description='Answer the question') So when you load the page, it shows the choices. I pick the choice, press submit and it says "this is not a valid choice" and it blanks out the select field. Then when

Flask-Admin: How to change model using on_model_change?

雨燕双飞 提交于 2019-12-10 10:48:45
问题 I'm trying to set a field's value to be based on another field in the same form: def on_model_change(form, model, is_created): model.textcolumn.data = model.textcolumn2.data Updating via the Flask-Admin interface raises no exceptions, but no changes were made to the value in model.textcolumn. Inspecting the "model" object, I also noticed this is not the same as the SQLAlchemy model used to generate the ModelView. How can I change model.textcolumn's value to model.textcolumn2's value? Is there

Flask App Using WTForms with SelectMultipleField

我是研究僧i 提交于 2019-12-10 04:03:13
问题 I have a Flask application that uses WTForms for user input. It uses a SelectMultipleField in a form. I can't seem to get the app to POST all items in the field when selected; it only sends the first item selected regardless of how many the user selects. The Flask documentation says this about the data sent from this field type, but I don't see this behavior: The data on the SelectMultipleField is stored as a list of objects, each of which is checked and coerced from the form input. Here's a

wtforms raise a validation error after the form is validated

送分小仙女□ 提交于 2019-12-10 02:56:35
问题 I have a registration form that collects credit card info. The workflow is as follows: User enters registration data and card data through stripe. The form is validated for registration data. If the form is valid, payment is processed. If payment goes through, it's all good, the user is registered and moves on. If the payment fails, i want to be able to raise a validation error on a hidden field of the form. Is that possible? Here's a the form submission code: def register(): form =

How to retrieve session data with Flask?

我的未来我决定 提交于 2019-12-09 14:40:56
问题 I have flask+wtforms application. I can see in login() user object stored as if user: if user.verify_password(form.password.data): flash('You have been logged in') user.logins += 1 db.session.add(History(user.uid)) db.session.commit() session['user'] = user Now I wanted to retrieve the user if 'user' in session: User=session.get('user') print User.nickname ###<< how to retrieve specific object member? It fails with message like : Instance <User at 0x8e5a64c> is not bound to a Session;

Can wtforms custom validator make a field optional?

爷,独闯天下 提交于 2019-12-09 10:36:16
问题 I'm using a custom validator to check a field is not empty if a check box is checked. It checks correctly but regardless it always still validating if the value is a number. Basically I need a field to stop validation under certain conditions of the form. Is there a way for the custom validator to stop validation on the field? 回答1: Yes, custom validators can control the validation flow just like the built-in Optional and Required validators. To control the validation flow, you use the

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

我们两清 提交于 2019-12-09 10:34:14
问题 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? 回答1: 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()], ) 回答2: If you're trying to remove a field you could look at the Removing Fields Per-instance in the documentation