1

I`m making a simple flask form application by wtforms. after giving a message for InputRequired() and Length to PasswordField, messages were not showing on the temp website.

this is app.py

from flask import Flask, render_template, request
from modules.forms import Login

app = Flask(__name__)
app.config['SECRET_KEY'] = 'this is my SEC_KEY'

@app.route('/', methods = ['POST', 'GET'])
def home():
    form = Login()
    return render_template('index.html', form = form)
@app.route('/show')
def show():
    return render_template('show.html')

if __name__ == "__main__":
    app.run()

this is forms.py in Module directory

from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired, Length
from flask_wtf import FlaskForm

class Login(FlaskForm):
    email = StringField(validators=[InputRequired(message='please input Email')])
    password = PasswordField(validators=[InputRequired(message='please input password'), Length(min=4, max=10, message='this is not Password')])
    submit = SubmitField('Enter')

and finally my index.html file template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="#">
        {{form.email(placeholder="Email Address")}}
        {% for err in form.email.errors %}
        <p style="color: red;">{{err}}</p>
        {% endfor %}
        <br>
        {{form.password(placeholder="Password")}}
        {% for err in form.email.errors %}
        <p style="color: red;">{{err}}</p>
        {% endfor %}
        <br>
        {{form.submit}}
    </form>
</body>
</html> 
h4med
  • 117
  • 7
  • 1
    [this link](https://stackoverflow.com/questions/54366587/wtforms-custom-validation-message-ignored) may help you bro. good luck. – Mahdi Firouzjah Mar 03 '21 at 19:45

0 Answers0