Forums

flask forgot password not working

I am unable to figure out what exactly is the issue here. I keep getting below error:. When I visit my website.com/forgot I am using flask-security for user login and registration. But I am using custom registration with some extra fields. I cannot see how that can link reset not work... From the error it looks like its not able to find the template? I do have a forgot_password template copied into my templates directory.

I have this enabled :

app.config['SECURITY_FORGOT_PASSWORD_TEMPLATE'] = True
app.config['SECURITY_SEND_PASSWORD_RESET_NOTICE_EMAIL'] = True
app.config['SECURITY_RECOVERABLE'] = True

forgot_password.html in templates/security has this code :

 {% from "security/_macros.html" import render_field_with_errors, render_field %}
{% include "security/_messages.html" %}
<h1>Send password reset instructions</h1>
<form action="{{ url_for_security('forgot_password') }}" method="POST" name="forgot_password_form">
  {{ forgot_password_form.hidden_tag() }}
  {{ render_field_with_errors(forgot_password_form.email) }}
  {{ render_field(forgot_password_form.submit) }}
</form>
{% include "security/_menu.html" %}

I get this error from the server:

23,113 :    return self.view_functions[rule.endpoint](**req.view_args)
2017-01-03 08:22:23,114 :  File "/home/susarla/.virtualenvs/mynew_env/local/lib/python2.7/site-packages/flask_security/decorators.py", line 225, in wrapper
2017-01-03 08:22:23,114 :    return f(*args, **kwargs)
2017-01-03 08:22:23,114 :  File "/home/susarla/.virtualenvs/mynew_env/local/lib/python2.7/site-packages/flask_security/views.py", line 262, in forgot_password
2017-01-03 08:22:23,114 :    **_ctx('forgot_password'))
2017-01-03 08:22:23,114 :  File "/home/susarla/.virtualenvs/mynew_env/local/lib/python2.7/site-packages/flask_security/core.py", line 447, in render_template
2017-01-03 08:22:23,114 :    return render_template(*args, **kwargs)
2017-01-03 08:22:23,114 :  File "/home/susarla/.virtualenvs/mynew_env/local/lib/python2.7/site-packages/flask/templating.py", line 133, in render_template
2017-01-03 08:22:23,114 :    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
2017-01-03 08:22:23,114 :  File "/home/susarla/.virtualenvs/mynew_env/local/lib/python2.7/site-packages/jinja2/environment.py", line 854, in get_or_select_template
2017-01-03 08:22:23,114 :    return self.select_template(template_name_or_list, parent, globals)
2017-01-03 08:22:23,114 :  File "/home/susarla/.virtualenvs/mynew_env/local/lib/python2.7/site-packages/jinja2/environment.py", line 830, in select_template
2017-01-03 08:22:23,114 :    for name in names:
2017-01-03 08:22:23,114 :TypeError: 'bool' object is not iterable

The error is basically saying that it's expecting something that is an iterable object (like a string), but it's getting a bool (that is, True or False). If you look at your config, you're specifying

app.config['SECURITY_FORGOT_PASSWORD_TEMPLATE'] = True
app.config['SECURITY_SEND_PASSWORD_RESET_NOTICE_EMAIL'] = True
app.config['SECURITY_RECOVERABLE'] = True

So you're setting all three of those config values to bools. If you take a look at the Flask-Security documentation, the first one should be a string specifying the location of the template, not a bool:

SECURITY_FORGOT_PASSWORD_TEMPLATE: Specifies the path to the template for the forgot password page. Defaults to security/forgot_password.html.

I'd suggest removing the line where you set SECURITY_FORGOT_PASSWORD_TEMPLATE, and moving your forgot_password.html file into the default place, which is a subdirectory of your templates directory called security.

thanks @giles that seems to work.

Excellent, thanks for confirming!