我有一个这样的多步骤表单:
{{ 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() }}
这是路线:
@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)
当我尝试不使用嵌套形式直接插入数据时,它可以完美工作。 但是,每当我尝试使用上述嵌套形式时,URL都会重定向而不会插入数据库。让我感到困惑的是,它只是重定向而没有任何错误消息。
所以,我的问题是,如果我们使用嵌套形式,是否可以向数据库中插入数据??
答案 0 :(得分:1)
通常,表单不能嵌套-Can you nest html forms?
具体来说,您是否查看了表单发布的数据?
为了避免在这种情况下嵌套表格,您可以将所有子表格包装在一个<form>
标记中,而无需具有多个表格标记甚至嵌套。