Closed cootook closed 2 months ago
Thanks for pointing this out - I am not sure given your post why this is happening - _fs_is_user_authenticated is injected as part of the context_processor - so should be set for any template. Can you give more info about how you are building the template and whether this is replacing a flask-security template (can you show your config?) or using this for an endpoint in your applications. Thanks.
@jwag956 I was using Flask-Security 5.3.3, I downloaded _menu.html template as it is and put it in my security folder. Right after that I got the error. I removed the template, the error gone. I checked, _fs_is_user_authenticated was not injected to context. As I updated Flask-Security to 5.4 _fs_is_user_authenticated was in the context, and error gone.
Ahh - you should see all the templates in your installed version - not take the tip of the repository which of course can be incompatible. Glad you got things working.
@jwag956 I am trying to recreate the case.
I downgraded Flask-Security to v5.3.3
I got the same error
[2024-04-19 20:17:54,078] ERROR in app: Exception on /register [POST] Traceback (most recent call last): File "C:\project\.venv\Lib\site-packages\flask\app.py", line 1473, in wsgi_app response = self.full_dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request rv = self.handle_user_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask\app.py", line 865, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask_security\decorators.py", line 632, in wrapper return f(*args, **kwargs) ^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask_security\views.py", line 337, in register return _security.render_template( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask_security\utils.py", line 1074, in default_render_template return render_template(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask\templating.py", line 150, in render_template return _render(app, template, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask\templating.py", line 131, in _render rv = template.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\jinja2\environment.py", line 1301, in render self.environment.handle_exception() File "C:\project\.venv\Lib\site-packages\jinja2\environment.py", line 936, in handle_exception raise rewrite_traceback_stack(source=source) File "C:\project\studio_app\templates\security\register_user.html", line 2, in top-level template code {% from "security/_macros.html" import render_field_with_errors, render_field, render_form_errors, render_field_errors %} ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\studio_app\templates\layout.html", line 2, in top-level template code {% block doc -%} File "C:\project\studio_app\templates\layout.html", line 5, in block 'doc' {%- block html %} File "C:\project\studio_app\templates\layout.html", line 37, in block 'html' {% block body -%} File "C:\project\studio_app\templates\layout.html", line 56, in block 'body' {% block content %} File "C:\project\studio_app\templates\security\register_user.html", line 23, in block 'content' {% include "security/_menu.html" %} ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\studio_app\templates\security\_menu.html", line 5, in top-level template code {% if _fs_is_user_authenticated(current_user) %} ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\jinja2\utils.py", line 83, in from_obj if hasattr(obj, "jinja_pass_arg"): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ jinja2.exceptions.UndefinedError: '_fs_is_user_authenticated' is undefined 127.0.0.1 - - [19/Apr/2024 20:17:54] "POST /register HTTP/1.1" 500 - [2024-04-19 20:21:50,334] ERROR in app: Exception on /register [POST] Traceback (most recent call last): File "C:\project\.venv\Lib\site-packages\flask\app.py", line 1473, in wsgi_app response = self.full_dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request rv = self.handle_user_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask\app.py", line 865, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask_security\decorators.py", line 632, in wrapper return f(*args, **kwargs) ^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask_security\views.py", line 337, in register return _security.render_template( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask_security\utils.py", line 1074, in default_render_template return render_template(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask\templating.py", line 150, in render_template return _render(app, template, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\flask\templating.py", line 131, in _render rv = template.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\jinja2\environment.py", line 1301, in render self.environment.handle_exception() File "C:\project\.venv\Lib\site-packages\jinja2\environment.py", line 936, in handle_exception raise rewrite_traceback_stack(source=source) File "C:\project\studio_app\templates\security\register_user.html", line 2, in top-level template code {% from "security/_macros.html" import render_field_with_errors, render_field, render_form_errors, render_field_errors %} ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\studio_app\templates\layout.html", line 2, in top-level template code {% block doc -%} File "C:\project\studio_app\templates\layout.html", line 5, in block 'doc' {%- block html %} File "C:\project\studio_app\templates\layout.html", line 37, in block 'html' {% block body -%} File "C:\project\studio_app\templates\layout.html", line 56, in block 'body' {% block content %} File "C:\project\studio_app\templates\security\register_user.html", line 23, in block 'content' {% include "security/_menu.html" %} ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\studio_app\templates\security\_menu.html", line 5, in top-level template code {% if _fs_is_user_authenticated(current_user) %} ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\project\.venv\Lib\site-packages\jinja2\utils.py", line 83, in from_obj if hasattr(obj, "jinja_pass_arg"): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ jinja2.exceptions.UndefinedError: '_fs_is_user_authenticated' is undefined 127.0.0.1 - - [19/Apr/2024 20:21:50] "POST /register HTTP/1.1" 500 -
I removed template _menu.html - the error is gone, the app works with built-in menu.
I looked through changes of _menu.html template and found commit 6b3795cfcb0f2544d92f1c8411aaf839c69c1577
May be I do something wrong (for example my base template is named layout.html
instead of base.html
)
To overwrite templates I have to
I created the template
/_menu.html
and copied code from Flask-security repo.When I ran it I got error:
I figured out that
_fs_is_user_authenticated
was added to the template on commitI used Flask-Security 5.3.
There should be a note/comment in the template
/_menu.html
about its compatibility with previous versions.