Dynamic Form with Flask

Asked

Viewed 57 times

0

Hello, I’m creating a website that consists of a teacher creating a quiz and sending it to students to answer, but I’m not finding a way to get the teacher to ask as many questions as he wants. At the moment the questionnaire created is totally static, with only 5 multiple choice questions

As this defined the fields in flask:

class NewForm(FlaskForm):
title = StringField("title", validators=[InputRequired()])
description = StringField("description", widget=TextArea(), validators=[InputRequired()])

q1desc = StringField("q1desc", widget=TextArea(), validators=[InputRequired()])
q2desc = StringField("q2desc", widget=TextArea(), validators=[InputRequired()])
q3desc = StringField("q3desc", widget=TextArea(), validators=[InputRequired()])
q4desc = StringField("q4desc", widget=TextArea(), validators=[InputRequired()])
q5desc = StringField("q5desc", widget=TextArea(), validators=[InputRequired()])
op11 = StringField("op11", validators=[InputRequired()])
op12 = StringField("op11", validators=[InputRequired()])
op13 = StringField("op11", validators=[InputRequired()])
op14 = StringField("op11", validators=[InputRequired()])
op21 = StringField("op11", validators=[InputRequired()])
op22 = StringField("op11", validators=[InputRequired()])
op23 = StringField("op11", validators=[InputRequired()])
op24 = StringField("op11", validators=[InputRequired()])
op31 = StringField("op11", validators=[InputRequired()])
op32 = StringField("op11", validators=[InputRequired()])
op33 = StringField("op11", validators=[InputRequired()])
op34 = StringField("op11", validators=[InputRequired()])
op41 = StringField("op11", validators=[InputRequired()])
op42 = StringField("op11", validators=[InputRequired()])
op43 = StringField("op11", validators=[InputRequired()])
op44 = StringField("op11", validators=[InputRequired()])
op51 = StringField("op11", validators=[InputRequired()])
op52 = StringField("op11", validators=[InputRequired()])
op53 = StringField("op11", validators=[InputRequired()])
op54 = StringField("op11", validators=[InputRequired()])

As the Sqlite table is defined (I omitted the init so it doesn’t get too big):

class Questionario(db.Model):
__tablename__ = "questionario"

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
titulo = db.Column(db.String)
descricao = db.Column(db.String)
tentativas = db.Column(db.Integer)
desc1 = db.Column(db.String)
resp1 = db.Column(db.Integer)
desc2 = db.Column(db.String)
resp2 = db.Column(db.Integer)
desc3 = db.Column(db.String)
resp3 = db.Column(db.Integer)
desc4 = db.Column(db.String)
resp4 = db.Column(db.Integer)
desc5 = db.Column(db.String)
resp5 = db.Column(db.Integer)
op11 = db.Column(db.String)
op12 = db.Column(db.String)
op13 = db.Column(db.String)
op14 = db.Column(db.String)
op21 = db.Column(db.String)
op22 = db.Column(db.String)
op23 = db.Column(db.String)
op24 = db.Column(db.String)
op31 = db.Column(db.String)
op32 = db.Column(db.String)
op33 = db.Column(db.String)
op34 = db.Column(db.String)
op41 = db.Column(db.String)
op42 = db.Column(db.String)
op43 = db.Column(db.String)
op44 = db.Column(db.String)
op51 = db.Column(db.String)
op52 = db.Column(db.String)
op53 = db.Column(db.String)
op54 = db.Column(db.String)

Full code on Github

The point is: How do I create new fields according to the amount the teacher wants? And how do I add new fields to the database table (Sqlite)?

1 answer

0


*For now this answer is incomplete, it covers only the problem of form definition, missing how to render the HTML and database mapping.

For the form, could do something with FieldList with FormField (documentation):

class Pergunta(Form):
    a = StringField("op11", validators=[InputRequired()])
    b = StringField("op12", validators=[InputRequired()])
    c = StringField("op13", validators=[InputRequired()])
    d = StringField("op14", validators=[InputRequired()])

class Prova(Form):
    perguntas = FieldList(FormField(Pergunta))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.