0

I have a multistep form like this:

{{ f.begin_form(form) }}
  {% if step == "input_student_email" %}
  <form action="{{ url_for('operator.add_schedule') }}" method="get">
    {{ f.render_field(form.student_email) }}
    <input type=hidden name="step" value="available_course">
    <input type="submit" value="Check available course">
  </form>
  {% elif step == "available_course" %}
  <form action="{{ url_for('operator.add_schedule') }}" method="get">
    {{ f.render_field(form.course_name) }}
    <input type=hidden name="step" value="type_of_class">
    <input type="submit" value="Check type of class">
  </form>
  {% elif step == "type_of_class" %}
  <form action="{{ url_for('operator.add_schedule') }}" method="get">
    {{ f.render_field(form.type_of_class) }}
    <input type=hidden name="step" value="input_schedule">
    <input type="submit" value="Input Schedule">
  </form>
  {% elif step == "input_schedule" %}
  <form action="{{ url_for('operator.add_schedule') }}" method="get">
    {{ f.render_field(form.schedule_month) }}
    {{ f.render_field(form.schedule_day) }}
    {{ f.render_field(form.start_at) }}
    {{ f.render_field(form.end_at) }}
    <input type=hidden name="step" value="input_teacher_email">
    <input type="submit" value="Check Teacher">
  </form>
  {% elif step == "input_teacher_email" %}
  <form action="{{ url_for('operator.add_schedule') }}" method="get">
    {{ f.render_field(form.teacher_email) }}
    <input type="submit" value="Submit">
  </form>
  {% endif %}
{{ f.end_form() }}

and here is the route:

@operator.route('/add-schedule', methods=['GET', 'POST'])
def add_schedule():
    form = ScheduleForm()
    if "step" not in request.form:
        return render_template('manipulate-schedule.html', form=form, step="input_student_email")
    elif request.form["step"] == "available_course":
        session['student_email'] = form.student_email.data
        return render_template('manipulate-schedule.html', form=form, step="available_course")
    elif request.form["step"] == "type_of_class":
        session['course_name'] = str(form.course_name.data)
        return render_template('manipulate-schedule.html', form=form, step="type_of_class")
    elif request.form["step"] == "input_schedule":
        session['type_of_class'] = form.type_of_class.data
        return render_template('manipulate-schedule.html', form=form, step="input_schedule")
    elif request.form["step"] == "input_teacher_email":
        if form.validate_on_submit():
            student_email = str(session.get('student_email'))
            course_name = str(session.get('course_name'))
            type_of_class = str(session.get('type_of_class'))
            schedule_month = form.schedule_month.data
            schedule_day = form.schedule_day.data
            start_at = str(form.start_at.data)
            end_at = str(form.end_at.data)
            teacher_email = form.teacher_email.data
            student = db.session.query(Student).filter_by(email=student_email).first()
            course = db.session.query(Course).filter_by(name=course_name).first()
            teacher = Teacher.query.filter_by(email=teacher_email).first()

            # test the result
            test_all_data = str(student_email) + str(course_name) + str(type_of_class) + str(schedule_month) + str(
            schedule_day) + str(start_at) + str(end_at)
            print('test', test_all_data)


            schedule = Schedule(
                student_id=student.id,
                course_id=course.id,
                type_of_class=type_of_class,
                schedule_month=schedule_month,
                schedule_day=schedule_day,
                start_at=start_at,
                end_at=end_at,
                teacher_id=teacher.id
            )
            db.session.add(schedule)
            try:
                db.session.commit()
            except Exception as e:
                db.session.rollback()
            return redirect(url_for('operator.all_schedules'))
        return render_template('manipulate-schedule.html', form=form, step="input_teacher_email")
    return render_template('manipulate-schedule.html', form=form)

When I try to insert the data directly without nested form, it works perfectly. But whenever I try to use a nested form like above, the URL just redirects without inserted to the database. The thing that makes me confuse is, it just redirect without any error message.

So, the point of my question is, is it posible to insert data to the database if we using nested form..?

Tri
  • 1,614
  • 2
  • 18
  • 44
  • 1
    shouldn't be `method="POST"` to get it as `request.form` ? – furas Sep 15 '19 at 08:32
  • 1
    You can always check `print(request.form, request.args, request.data)` to see what was send to flask. – furas Sep 15 '19 at 08:32
  • Hi @furas, thanks for your reply, I have try to check the data, like my edited question, and yea.. all of the data was send to the end of form. – Tri Sep 15 '19 at 09:02

1 Answers1

2

Generally, forms could not be nested - Can you nest html forms?

Specifically, did you look at the data that are being POSTed by the form? To avoid nesting forms in this case, you can wrap all subforms in the one <form> tag without any need to have multiple form tags or even nesting them.

Kryštof Řeháček
  • 1,171
  • 9
  • 21