-1

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?

Liumx31
  • 1,190
  • 1
  • 16
  • 33
  • `ResetPasswordRequestForm` reads the fields from the `request` object, if they were sent from the HTML code. That object is just a container for the fields. If there were no fields, then it generates the input HTML/. – Tim Roberts Jun 07 '21 at 05:35
  • @TimRoberts thanks! is that a built-in capability of FlaskForm? – Liumx31 Jun 07 '21 at 06:07

2 Answers2

0
{% for error in form.email.errors %}
<span style="color: red;">[{{ error }}]</span>

If you don't pass any information it will return validation Error.

George Imerlishvili
  • 1,816
  • 2
  • 12
  • 20
0

If you're primarily using blueprint, you can write your code much easier than that. https://flask.palletsprojects.com/en/2.0.x/tutorial/views/

Now to solve your problem, you can declare your action method i think: html :

<form action="{{ url_for('your_endpoint.function_name') }}" >

or:

<form action="{{ url_for('app.reset_password_request') }}" >

and:

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():
try:
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = ResetPasswordRequestForm()
    # if form is not None:
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        # if user is not None: # If the table is empty, it will return None. Not boolean.
        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)
except Exception as e:
  print(e)
finally:
  pass # If you wish, you can redirect to another page when the process is finished.
Ayse
  • 576
  • 4
  • 13