我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用flask_wtf.FlaskForm()。
def __init__(self, records, precedent, *args, **kwargs): #only_csrf = MultiDict([('csrf_token', precedent.get('csrf_token'))]) #super(FlaskForm, self).__init__(only_csrf, *args, **kwargs) super(FlaskForm, self).__init__(precedent, *args, **kwargs) genres_choices = [(x, x) for x in records.genres] no_genres_choices = genres_choices self.genres.choices = genres_choices self.no_genres.choices = no_genres_choices artists_choices = [(x, x) for x in records.artists] self.artists.choices = artists_choices self.no_artists.choices = artists_choices keywords_choices = [(x, x) for x in records.keywords] self.keywords.choices = keywords_choices self.no_keywords.choices = keywords_choices titles_choices = [(x, x) for x in records.titles] self.titles.choices = titles_choices self.no_titles.choices = titles_choices albums_choices = [(x, x) for x in records.albums] self.albums.choices = albums_choices self.no_albums.choices = albums_choices
def generate_household_composition_form(block_json, data, error_messages): class HouseHoldCompositionForm(FlaskForm): question_errors = {} household = FieldList(FormField(get_name_form(block_json, error_messages)), min_entries=1) def map_errors(self): ordered_errors = [] if 'household' in self.errors and self.errors['household']: for index, field in enumerate(self.household): ordered_errors += map_field_errors(field.errors, index) return ordered_errors def answer_errors(self, input_id): return [error[1] for error in self.map_errors() if input_id == error[0]] def remove_person(self, index_to_remove): popped = [] while index_to_remove != len(self.household.data): popped.append(self.household.pop_entry()) popped.reverse() for field in popped[1:]: self.household.append_entry(field.data) def serialise(self, location): """ Returns a list of answers representing the form data :param location: The location to associate the form data with :return: """ return serialise_composition_answers(location, self.household.data) return HouseHoldCompositionForm(MultiDict(data))
def _get_context(full_routing_path, block, current_location, form): metadata = get_metadata(current_user) answer_store = get_answer_store(current_user) schema_context = _get_schema_context(full_routing_path, current_location, metadata, answer_store) variables = g.schema_json.get('variables') if variables: variables = renderer.render(variables, **schema_context) if block['type'] == 'Summary': rendered_schema_json = renderer.render(g.schema_json, **schema_context) form = form or FlaskForm() summary_rendered_context = build_summary_rendering_context(rendered_schema_json, answer_store, metadata) context = {'form': form, 'summary': summary_rendered_context, 'variables': variables} return context block = renderer.render(block, **schema_context) context = {'block': block, 'variables': variables} if not form: error_messages = SchemaHelper.get_messages(g.schema_json) form, template_params = get_form_for_location(block, current_location, answer_store, error_messages) if template_params: context.update(template_params) context['form'] = form return context
def generate_relationship_form(block_json, number_of_entries, data, error_messages): answer = SchemaHelper.get_first_answer_for_block(block_json) class HouseHoldRelationshipForm(FlaskForm): question_errors = {} def map_errors(self): ordered_errors = [] if self.errors: for answer_id, error_list in self.errors.items(): for errors in error_list: for error in errors: ordered_errors.append((answer_id, error)) return ordered_errors def answer_errors(self, input_id): return [error[1] for error in self.map_errors() if input_id == error[0]] def serialise(self, location): """ Returns a list of answers representing the form data :param location: The location to associate the form data with :return: """ list_field = getattr(self, answer['id']) return serialise_relationship_answers(location, answer['id'], list_field.data) choices = [('', 'Select relationship')] + build_choices(answer['options']) field = FieldList(SelectField( label=answer.get('guidance'), description=answer.get('label'), choices=choices, default='', validators=get_mandatory_validator(answer, error_messages, 'MANDATORY_TEXTFIELD'), ), min_entries=number_of_entries) setattr(HouseHoldRelationshipForm, answer['id'], field) if data: form = HouseHoldRelationshipForm(MultiDict(data)) else: form = HouseHoldRelationshipForm() return form