[Python] Errors with Flask-WTF, validators

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    The code is as follows:

    wtf.py:

    from flask import Flask, render_template, redirect, url_for, request, flash
    from forms import MyForm
    from flask_wtf import CSRFProtect

    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'KERNEL?::(65535)/TCP.SEC'
    csrf = CSRFProtect(app)
    @app.route('/home', methods=['GET','POST'])
    def home():
    form = MyForm()#Se crea una instancia de la clase de formulario.
    if form.validate_on_submit():
    return 'User: {form.user.data}<br>Password: {form.password.data}<br>'
    print(request.method)
    print(form.validate_on_submit())
    return render_template('wtf.html', form=form)

    @app.route('/profile', methods=['GET', 'POST'])
    def profile():
    if request.method == 'POST':
    username = request.form.get('name')
    password = request.form.get('password')
    return f'Username: {username}<br>Password: {password}'
    else:
    return 'Nothing to show here, bro.'
    if __name__ == '__main__':
    app.run(debug=True)


    wft.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WTForm</title>
    <style>
    body{
    background-color: #2B2D42;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 0;
    height: 100vh;
    }
    *{
    color: #92DCE5;
    }
    .main{
    font-size: 1.5em;
    font-weight: 1500;
    }
    form p input{
    color: black;
    }
    form p label{
    color: #FF66D8;
    }
    form p input:focus{
    outline: none;
    }
    </style>
    </head>
    <body>
    <p class="main">Flask WTF</p>
    <form action="{{ url_for('profile') }}" method="post">
    {{ form.hidden_tag() }}
    <p>{{ form.name.label }} {{ form.name() }}</p>
    <p>{{ form.password.label }} {{ form.password() }}</p>
    <p>{{ form.password_confirmation.label }} {{ form.password_confirmation }}</p>
    <p>{{ form.submit() }}</p>
    </form>
    </body>
    </html>


    form.py:

    from flask_wtf import FlaskForm
    from wtforms import StringField, SubmitField, PasswordField
    from wtforms.validators import DataRequired, length, EqualTo

    class MyForm(FlaskForm):
    name = StringField('Name', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired(),length(min=8, max=16)])
    password_confirmation = PasswordField('Confirm', validators=[DataRequired(), EqualTo('password', message='Must be equal to first password')])
    submit = SubmitField('Send username')



    The main problem is that the EqualTo() validator (in form.py) doesn't work as it's supposed to since it doesn't throw any warning if the password is incorrect (which by the way, I already tried) and the page is simply submitted, but another problem is that the form should send the request as a POST, however, in the /home view I can't capture that method (I think that's the problem).

    Additional information:

    Flask 3.1.0
    Flask-WTF 1.2.2
    Werkzeug 3.1.3
    WTForms 3.2.1

    Continue reading...

Compartilhe esta Página