xx.html
{% extends 'base.html' %} {% from 'macros.html' import form_field %}
{% block content %} <h2>Multiple Form in One Page with Multiple View</h2>
<h3>Login Form</h3> <form method="post" action="{{ url_for('handle_signin') }}"> {{ signin_form.csrf_token }} {{ form_field(signin_form.username) }} {{ form_field(signin_form.password) }} {{ signin_form.submit }} </form>
<h3>Register Form</h3> <form method="post" action="{{ url_for('handle_register') }}"> {{ register_form.csrf_token }} {{ form_field(register_form.username) }} {{ form_field(register_form.email) }} {{ form_field(register_form.password) }} {{ register_form.submit }} </form> {% endblock %}
form.py
class SigninForm2(FlaskForm): username = StringField('Username', validators=[DataRequired(), Length(1, 24)]) password = PasswordField('Password', validators=[DataRequired(), Length(8, 128)]) submit = SubmitField()
class RegisterForm2(FlaskForm): username = StringField('Username', validators=[DataRequired(), Length(1, 24)]) email = StringField('Email', validators=[DataRequired(), Email(), Length(1, 254)]) password = PasswordField('Password', validators=[DataRequired(), Length(8, 128)]) submit = SubmitField()
app.py
@app.route('/multi-form-multi-view') def multi_form_multi_view(): signin_form = SigninForm2() register_form = RegisterForm2() return render_template('2form2view.html', signin_form=signin_form, register_form=register_form)
@app.route('/handle-signin', methods=['POST']) def handle_signin(): signin_form = SigninForm2() register_form = RegisterForm2()
代码语言:javascript复制if signin_form.validate_on_submit():
username = signin_form.username.data
flash('%s, you just submit the Signin Form.' % username)
return redirect(url_for('index'))
return render_template('2form2view.html', signin_form=signin_form, register_form=register_form)
@app.route('/handle-register', methods=['POST']) def handle_register(): signin_form = SigninForm2() register_form = RegisterForm2()
代码语言:javascript复制if register_form.validate_on_submit():
username = register_form.username.data
flash('%s, you just submit the Register Form.' % username)
return redirect(url_for('index'))
return render_template('2form2view.html', signin_form=signin_form, register_form=register_form)