4

I'm trying to display the errors flask-security should generate when there's a failed login, or improper registration or whatever, but it really appears the errors are not even generated in my project.

I'm using a custom form helper display template that looks like this (my_render_field):

{{ field(class="w3-input hidden-print w3-white", **kwargs)|safe }}
{% for error in field.errors %}
    <div class="w3-panel w3-red w3-display-container">
        <span onclick="this.parentElement.style.display='none'"class="w3-button w3-red w3-large w3-display-topright">&times;</span>
            <h3>Error!</h3>
            <p> {{ error }}</p>
    </div>
{% endfor %}

I have this thing work for everything but the fields generated by flask-security. I have a custom login form that looks like this:

<form action="{{ url_for_security('login') }}" method="POST" name="login_form">
      {{ login_form.hidden_tag() }}
      {{ my_render_field(login_form.email, placeholder='Email') }}
      {{ my_render_field(login_form.password, placeholder='Password') }}
        <div>
      <input type="checkbox" class="w3-check" value="Remember"><label>{{ gettext('Remember Me') }}</label></input>
            </div>
      <input type="submit" class="w3-button w3-blue" value="Submit">
    </form>

The login form is defined like this:

from flask_security import LoginForm

@security.login_context_processor
def security_login_processor():
    """ Simple function to inject flask-security's LoginForm into my login_form 
    This function also generates the localised title """
    return dict(login_form=LoginForm(), title=gettext('Sign In'))

However, after making sure SECURITY_FLASH_MESSAGES is explicitly set to true, I try to include flashed messages like this:

{% with messages = get_flashed_messages(with_categories=true) %}
    {% if messages %}
        {% for category, message in messages %}
            <p> {{ category }} - {{ message }}</p>
        {% endfor %}
    {% endif %}
{% endwith %}

And I never get any output.

EDIT:

It now appears that the fields from LoginForm() don't have validators? When I use login_user_form in my view, it works as expected, but that means I can't use a modified form.

S.Comeau
  • 61
  • 9
  • 1
    Flask security handles errors by appending them to each WTForms field. Only success messages look to be flashed, everything else is displayed via the field. If you look into the template, you will find it extends a macro that handles displaying the error. – Rushil Srivastava Apr 15 '20 at 11:19

0 Answers0