Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 缓存当前用户已通过身份验证_Python_Flask_Jinja2_Flask Login - Fatal编程技术网

Python 缓存当前用户已通过身份验证

Python 缓存当前用户已通过身份验证,python,flask,jinja2,flask-login,Python,Flask,Jinja2,Flask Login,如果我有一个模板,其多次验证为{%If current_user.is_authenticated%},那么使用类似的方法来最小化开销是有意义的,然后只需检查current_user\u is_authenticated {% if current_user.is_authenticated %} {% set current_user_is_authenticated = True %} {% else %} {% set current_user_is_authenticat

如果我有一个模板,其多次验证为
{%If current_user.is_authenticated%}
,那么使用类似的方法来最小化开销是有意义的,然后只需检查
current_user\u is_authenticated

{% if current_user.is_authenticated %}
    {% set current_user_is_authenticated = True %}
{% else %}
    {% set current_user_is_authenticated = False %}
{% endif %}

首先,这取决于您如何在用户类上实现
is\u authenticated
方法。 默认情况下,如果当前用户登录,它总是只返回
True

回答您的问题:如果您没有自定义的
is\u authenticated
方法实现,那么就没有一件事可以最小化开销。但是,如果您在
中查询db、进行外部api调用等。在模板中多次使用它时,将其返回值存储在变量中是一种优化