0
votes

I'm making a web application using Python Flask. I have two Flask-WTF to edit my components on a page:

class WorkEDForm(FlaskForm):
    date = StringField("Date", validators=[DataRequired()])
    name = StringField("Name", validators=[DataRequired()])
    details = StringField("Details", validators=[DataRequired()])


class PersonalForm(FlaskForm):
    name = StringField("Name", validators=[DataRequired()])
    details = StringField("Details", validators=[DataRequired()])

And this is a part from my html code which corelates with these form:

    {% for ed in education %}
    <div class="ed">
      <p>{{ed.date}}</p>
      <br>
      <h4>{{ed.schol_name}}</h4>
      <p>{{ed.details}}</p>
    </div>

    {% endfor %}

  </div>

  <div class="language-skills">
    <h2>Language Skills</h2>
    <hr>

    {% for language in languages %}
    <div class="language">
      <h4>{{language.language_name}}</h4>
      <p>{{language_name.details}}</p>
    </div>
    {% endfor %}

As you can see I store all of my data in SQLAlchemy database, but I want to be able to edit or make it with forms. I want to make a function which will allow me to do it but in order to do that i have to create a correct form inside my function because one of the html elements contains the date and the other doesn't. Here's a 'skeleton' of my function for better undersanding:

@app.route("/make", methods=["GET", "POST"])

    def make():
        # if this is element related to this form:
        #     create related form
        # else:
        #     but if its related to the second one create related form

Is there a way for if statement, to distinguish which html element i want to edit, or make in order for it to create a correct form?

1

1 Answers

1
votes

I'm not sure if I understood your question correctly, but if I did. You could do the following to build your form dynamically based on sending this form building function

def buildForm(include_date_boolean):
    class FormName(FlaskForm):
        name = StringField("Name")
        details = StringField("Name")
        pass

    if include_date_boolean:
        setattr(FormName, 'date', StringField(label="Date"))
    return FormName()