可变长度元素的动态形式:wtforms [英] Dynamic forms from variable length elements: wtforms

查看:62
本文介绍了可变长度元素的动态形式:wtforms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wtforms,我需要创建一个东西,该东西将基于数据库中的信息生成表单定义.动态表单创建.我已经知道需要做什么,而且我才刚刚开始.我可以创建表格并将其与wtforms/flask一起使用,但是从数据中定义表格(每个表格之间会有细微的差异)目前超出了我目前的技能水平.

I'm using wtforms, and I need to create a something that will generate a form definition based off information in a database; dynamic form creation. I'm getting a sense of what needs to be done and I've just started. I can create forms and use them with wtforms/flask but defining forms from data that will vary slightly from form to form is currently beyond my current skill level.

有人这样做并提供一些意见吗?有点模糊的问题,尚无实际代码.我还没有找到任何例子,但这并不是没有可能.

Has anyone done this and have some input to offer? Somewhat a vague question, no actual code yet. I haven't found any examples, but it is not impossible to do.

mass of variable data to be used in a form --> wtforms ---> form on webpage

因此,例如,我们可以使用调查.一个调查由几个SQLAlcehmy模型组成.调查是具有任何数量的关联问题模型的模型(问题属于调查,并且对于多项选择题来说,它变得很复杂).为简化起见,我们将简单的json/dict伪代码用于:

So, a 'for example' we can use surveys. A survey consists of several SQLAlcehmy models. A survey is a model with any number of associated questions models(questions belong to surveys and it gets complicated for say, multiple choice questions). To simplify let's use simple json/dict pseudo code for:

{survey:"Number One",
    questions:{
        question:{type:truefalse, field:"Is this true or false"},
        question:{type:truefalse, field:"Is this true or false"},
        question:{type:text, field:"Place your X here"}
     } 
 }

{survey:"Number Two",
    questions:{
        question:{type:text, field:"Answer the question"},
        question:{type:truefalse, field:"Is this true or false"},
        question:{type:text, field:"Place your email address here"}
     } 
 }

而不是想象一下,具有5种以上字段类型的数百种不同长度.如何使用WTForms来管理表单,或者我什至需要使用wtforms?我可以根据需要定义静态表单,但不能动态定义.

Imagine instead of this, several hundred of varying lengths with 5+ field types. How to use WTForms to manage forms for this, or do I even need to use wtforms? I can define static forms as I need them, but not dynamically, yet.

顺便说一句,我已经用简单的形式在rails中完成了类似的事情,但是当我在Python atm中工作时(在其他方面,我以调查为例,但是问题/领域/答案为例)我需要的许多输入类型的摘要).

As an aside I've done something like this in rails with simpleform but as I'm working in Python atm (on something different, I'm using the survey thing as an example, but the question/field/answer thing abstracts across a many types of inputs I've needed).

所以是的,我可能需要建造某种工厂,这样做会花一些时间,例如:

So yes it is possible I'll need to build some sort of factory, doing it will take me some time e.g.:

http://wtforms.simplecodes.com/docs/1.0.2/specific_problems.html

https://groups.google.com/forum/? fromgroups =#!topic/wtforms/cJl3aqzZieA

推荐答案

在运行时将适当的字段简单地添加到基本表单中.这是您如何做的草图(尽管已大大简化):

Simply add the appropriate fields to the base form at run time. Here's a sketch of how you might do it (albeit much simplified):

class BaseSurveyForm(Form):
    # define your base fields here


def show_survey(survey_id):
    survey_information = get_survey_info(survey_id)

    class SurveyInstance(BaseSurveyForm):
        pass

    for question in survey_information:
        field = generate_field_for_question(question)
        setattr(SurveyInstanceForm, question.backend_name, field)

    form = SurveyInstanceForm(request.form)

    # Do whatever you need to with form here


def generate_field_for_question(question):
    if question.type == "truefalse":
        return BooleanField(question.text)
    elif question.type == "date":
        return DateField(question.text)
    else:
        return TextField(question.text)

这篇关于可变长度元素的动态形式:wtforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆