[Python] Can't visualize the validation error on flask web server

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to show a validation error to the users when they try to sign up with an email or username that's already been taken.

    However, despite the fact it works, but I can't see anything on the webpage when I run the web development server.

    Here is the Python code for the validation error:

    from flask_wtf import FlaskForm
    from wtforms import StringField, PasswordField, SubmitField, BooleanField
    from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
    from flaskblog.models import User


    class RegistrationForm(FlaskForm):
    username = StringField('Username',
    validators=[DataRequired(), Length(min=2, max=20)])
    email = StringField('Email',
    validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    confirm_password = PasswordField('Confirm Password',
    validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Sign Up')

    def validate_username(self, username):
    user = User.query.filter_by(username=username.data).first()
    if user:
    raise ValidationError('That username is taken. Please choose a different one.')

    def validate_email(self, email):
    email = User.query.filter_by(email=email.data).first()
    if email:
    raise ValidationError('That email is taken. Please choose a different one.')


    class LoginForm(FlaskForm):
    email = StringField('Email',
    validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    remember = BooleanField('Remember Me')
    submit = SubmitField('Login')


    But even if the outline is red, I don't see the message:

    [​IMG]

    I'm not sure if it's useful, but here's also the layout.html code I use for generic rendering of pages:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}">

    {% if title %}
    <title>Flask Blog - {{ title }} </title>
    {% else %}
    <title>Flask Blog</title>
    {% endif %}
    </head>
    <body>
    <header class="site-header">
    <nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
    <div class="container">
    <a class="navbar-brand mr-4" href="/">Flask Blog</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarToggle">
    <div class="navbar-nav mr-auto">
    <a class="nav-item nav-link" href="{{ url_for('home') }}">Home</a>
    <a class="nav-item nav-link" href="{{ url_for('about') }}">About</a>
    </div>
    <!-- Navbar Right Side -->
    <div class="navbar-nav">
    <a class="nav-item nav-link" href="{{ url_for('login') }}">Login</a>
    <a class="nav-item nav-link" href="{{ url_for('register') }}">Register</a>
    </div>
    </div>
    </div>
    </nav>
    </header>
    <main role="main" class="container">
    <div class="row">
    <div class="col-md-8">
    {% with messages = get_flashed_messages(with_categories=true) %}
    {% if messages %}
    {% for category, message in messages %}
    <div class="alert alert-{{ category }}">
    {{ message }}
    </div>
    {% endfor %}
    {% endif %}
    {% endwith %}
    {% block content %}{% endblock %}

    </div>
    <div class="col-md-4">
    <div class="content-section">
    <h3>Our Sidebar</h3>
    <p class='text-muted'>You can put any information here you'd like.
    <ul class="list-group">
    <li class="list-group-item list-group-item-light">Latest Posts</li>
    <li class="list-group-item list-group-item-light">Announcements</li>
    <li class="list-group-item list-group-item-light">Calendars</li>
    </ul>
    </p>
    </div>
    </div>
    </div>
    </main>


    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

    </body>
    </html>

    Continue reading...

Compartilhe esta Página