1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # 表单模型结构 需要配合 render_template使用 class NewNoteForm(FlaskForm): # Body字段必填 body=TextAreaField('Body',validators=[DataRequired()]) submit=SubmitField('Save')
@app.route('/new',methods=['GET','POST']) def new_note(): form = NewNoteForm() # 如果点击提交按钮 保存数据 if form.validate_on_submit(): body=form.body.data note=Note(body=body) db.session.add(note) db.session.commit() # 模版文件 有flash显示提示 flash('Your note is saved!') # 新增成功 跳转到首页 hello 指得是app.route('/') 包围的函数名 return redirect(url_for('hello')) 否则 显示表单页面 return render_template('new_note.html',form=form)
|