from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, SubmitField from wtforms.validators import DataRequired, Email class ResetPasswordForm(FlaskForm): email = StringField('Email', validators=[DataRequired(), Email()]) password = PasswordField('Password', validators=[DataRequired()]) confirm_password = PasswordField('Confirm Password', validators=[DataRequired()]) submit = SubmitField('Reset Password')
@app.route('/reset_password', methods=['GET', 'POST']) def reset_password(): form = ResetPasswordForm() if form.validate_on_submit(): # Reset password flash('Password reset successful') return redirect(url_for('login')) return render_template('reset_password.html', form=form)In this example, we have created a route for resetting the user's password. We have instantiated the ResetPasswordForm class and passed it to the template. When the user submits the form, the validate_on_submit method is called. If the form is valid, the password is reset and a success message is flashed to the user. If the form is not valid, the form is displayed again with the appropriate error messages. The package library used in this example is Flask-WTF, which is a Flask extension that provides integration with the WTForms library.