Please forgive me if this question is too elementary, I just started learning Flask and am getting a little bit confused by what's happening behind the scenes in routing. For example, I'm following Miguel Grinberg's tutorial, and here is how he creates the reset password page and routing method:
view:
{% extends "base.html" %}
{% block content %}
<h1>Reset Password</h1>
<form action="" method="post">
{{ form.hidden_tag() }}
<p>
{{ form.email.label }}<br>
{{ form.email(size=64) }}<br>
{% for error in form.email.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>{{ form.submit() }}</p>
</form>
{% endblock %}
route:
from app.forms import ResetPasswordRequestForm
from app.email import send_password_reset_email
@app.route('/reset_password_request', methods=['GET', 'POST'])
def reset_password_request():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = ResetPasswordRequestForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user:
send_password_reset_email(user)
flash('Check your email for the instructions to reset your password')
return redirect(url_for('login'))
return render_template('reset_password_request.html',
title='Reset Password', form=form)
My understanding is, if the user clicks the submit button on the webpage, the information in the form would get sent to the routing function, get validated and generate an email using the other user-defined functions, however, I see a form = ResetPasswordRequestForm()
in the routing function, which seems to suggest that a new instance of the password reset form is created in the routing function -- how does the form get validated if the information isn't passed from the view to the routing function?