0
Good afternoon guys, all right? I created a form using the flaskform class, from flask_wtf. I can send it to the front of the application and apparently the behavior is correct. However, when I submit it to do the validation, I end up not being able to "fall" in the block of it. The behavior that I would expect in app.py, when completing and submitting the data would be to fall into regForm.validate_on_submit() and thus give a redirect to the home. However, I am "falling" under the other condition (print "we are here 2") and rendering the admin-Register template (see app.py code below). Could someone please help me? I do not find the mistake I have searched in several ways. I am using the following versions of the technologies:
Python 3.7.4 ; email-Validator==1.1.2 ; Flask==1.1.2 ; Flask-Assets==2.0 ; Flask-WTF==0.14.3 ; Wtforms==2.3.3
Return when submitting the form
app file.py
from flask import Flask,flash, jsonify, request, render_template, url_for, redirect
from flask_assets import Environment, Bundle
from authforms import RegistrationForm, LoginForm
import json, secrets
from os import environ
######## APP START ########
#initialize the app
app = Flask(__name__)
#routes - Web
@app.route('/', methods=['GET'])
def home():
homeTitle = 'O melhor buscador de CID'
return render_template('index.html', title=homeTitle)
#routes - Web (Admin)
@app.route('/cadastro-admin', methods=['GET', 'POST'])
def admin_register():
regForm = RegistrationForm(request.form)
if regForm.validate_on_submit():
flash(f'Conta criada para {regForm.adminName.data} :)', 'message_success')
return redirect(url_for('home'))
registerTitle = "Cadastro para administradores do sistema de buscas inteligentes de CID"
print("we are here 2")
return render_template('admin-register.html', title=registerTitle, form=regForm)
#run the application
if __name__ == '__main__':
app.run(debug=True)
authforms.py file (class responsible for registration and login forms):
from flask_wtf import FlaskForm
from wtforms import Form, StringField, PasswordField, SubmitField, BooleanField, validators
from wtforms.validators import Email, Length, DataRequired, EqualTo
import email_validator
class RegistrationForm(FlaskForm):
adminName = StringField('Nome', validators=[DataRequired(), Length(min=3, max=30)], render_kw={'placeholder': 'Nome:'})
adminSurname = StringField('Sobrenome', validators=[DataRequired(), Length(min=3, max=30)], render_kw={'placeholder': 'Sobrenome:'})
adminEmail = StringField('Email', validators=[DataRequired(), Email()],render_kw={'placeholder': 'E-mail:'})
adminPass = PasswordField('Senha', validators=[DataRequired(), Length(min=6, max = 20)], render_kw={'placeholder': 'Senha:'})
confirmPass = PasswordField('Confirme a Senha', validators=[DataRequired(), EqualTo('password')], render_kw={'placeholder': 'Confirmação da senha:'})
submit = SubmitField('Cadastrar')
HTML file where the form is being rendered
{% extends "base.html" %} {% block content %}
<header>
<h1>Será um administrador do sistema? Cadastre-se aqui</h1>
</header>
<article>
<form method="POST" action="" name="admin_register_form" enctype="multipart/form-data">
{{form.hidden_tag()}}
{%if form.adminName.errors%}
{{form.adminName(class="registration_form_input")}}
<div style="display:block; border: 1px solid red">
{%for error in form.adminName.errors%}
<span>{{error}}</span>
{%endfor%}
</div>
{%else%}
{{form.adminName(class="registration_form_input")}}
{%endif%}
{{form.adminSurname(class="registration_form_input")}}
{{form.adminEmail(class="registration_form_input")}}
{{form.adminPass(class="registration_form_input")}}
{{form.confirmPass(class="registration_form_input")}}
{{form.submit(class="registration_form_input")}}
</form>
<small>Possui cadastro? <a href="{{url_for('admin_login')}}">Login</a></small>
</article>
{% endblock content %}
It helped me a lot! I managed to find the error. Thank you very much!
– Gustavo Pinheiro