flaskform pass a variable (WTForms)

前端 未结 2 1175
一个人的身影
一个人的身影 2021-01-28 18:24

I want to pass a str to be used as the prompt for a form. I thought it would be simple but it is proving to be difficult.

Here is my code:

class PostFo         


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-28 19:00

    Today I'm figured out about a similar problem as yours. I wanted to pass a variable to FlaskForm. For a small CV creation app, I want to give the user an opportunity to create more than 1 entry for his work experience and I wanted to do it with FieldList and FormField. And I needed to do it on one page so in one form.

    My solution is pretty simple python implementation of factory pattern for forms:

    class ConstructorForm(FlaskForm):
        ...
        work_experience_form = FieldList(FormField(WorkExperienceForm), min_entries=1, max_entries=1)
        skills_form = FieldList(FormField(SkillsForm), min_entries=1, max_entries=1)
        ...
    

    And here is my function for building extending forms:

    def constructor_form(work_experience_forms=1, skills_forms=1):
        class _ConstructorForm(ConstructorForm):
            pass
    
        _ConstructorForm.work_experience_form = FieldList(
            FormField(WorkExperienceForm), min_entries=work_experience_forms, max_entries=work_experience_forms
        )
        _ConstructorForm.skills_form = FieldList(
            FormField(SkillsForm), min_entries=skills_forms, max_entries=skills_forms
        )
    
        return _ConstructorForm()
    

提交回复
热议问题