python - flask中在模板中使用current_user.is_authenticated,运行报错,提示没定义
问题描述
flask中在模板中使用current_user.is_authenticated,运行报错,提示没定义报错信息Traceback (most recent call last): File 'E:PythonPython35-32libsite-packagesflaskapp.py', line 2000, in __call__ return self.wsgi_app(environ, start_response) File 'E:PythonPython35-32libsite-packagesflaskapp.py', line 1991, in wsgi_app response = self.make_response(self.handle_exception(e)) File 'E:PythonPython35-32libsite-packagesflaskapp.py', line 1567, in handle_exception reraise(exc_type, exc_value, tb) File 'E:PythonPython35-32libsite-packagesflask_compat.py', line 33, in reraise raise value File 'E:PythonPython35-32libsite-packagesflaskapp.py', line 1988, in wsgi_app response = self.full_dispatch_request() File 'E:PythonPython35-32libsite-packagesflaskapp.py', line 1641, in full_dispatch_request rv = self.handle_user_exception(e) File 'E:PythonPython35-32libsite-packagesflaskapp.py', line 1544, in handle_user_exception reraise(exc_type, exc_value, tb) File 'E:PythonPython35-32libsite-packagesflask_compat.py', line 33, in reraise raise value File 'E:PythonPython35-32libsite-packagesflaskapp.py', line 1639, in full_dispatch_request rv = self.dispatch_request() File 'E:PythonPython35-32libsite-packagesflaskapp.py', line 1625, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File 'D:1filepythontestblogblog.py', line 90, in login return render_template('/login.html', form=form) File 'E:PythonPython35-32libsite-packagesflasktemplating.py', line 134, in render_template context, ctx.app) File 'E:PythonPython35-32libsite-packagesflasktemplating.py', line 116, in _render rv = template.render(context) File 'E:PythonPython35-32libsite-packagesjinja2environment.py', line 989, in render return self.environment.handle_exception(exc_info, True) File 'E:PythonPython35-32libsite-packagesjinja2environment.py', line 754, in handle_exception reraise(exc_type, exc_value, tb) File 'E:PythonPython35-32libsite-packagesjinja2_compat.py', line 37, in reraise raise value.with_traceback(tb) File 'D:1filepythontestblogtemplateslogin.html', line 1, in top-level template code {% extends 'base.html'%} File 'D:1filepythontestblogtemplatesbase.html', line 1, in top-level template code {% extends 'bootstrap/base.html' %} File 'E:PythonPython35-32libsite-packagesflask_bootstraptemplatesbootstrapbase.html', line 1, in top-level template code {% block doc -%} File 'E:PythonPython35-32libsite-packagesflask_bootstraptemplatesbootstrapbase.html', line 4, in block 'doc' {%- block html %} File 'E:PythonPython35-32libsite-packagesflask_bootstraptemplatesbootstrapbase.html', line 20, in block 'html' {% block body -%} File 'E:PythonPython35-32libsite-packagesflask_bootstraptemplatesbootstrapbase.html', line 21, in block 'body' {% block navbar %} File 'D:1filepythontestblogtemplatesbase.html', line 12, in block 'navbar' {% if current_user.is_authenticated %} File 'E:PythonPython35-32libsite-packagesjinja2environment.py', line 408, in getattr return getattr(obj, attribute)jinja2.exceptions.UndefinedError: ’current_user’ is undefined
问题解答
回答1:发现是初始化配置少了一句
login_manager.init_app(app)
加上去就好了
回答2:current_user好像是flask-login这个插件注入的吧
相关文章:
1. php代码运行报错2. android - React Native 安卓运行报错 java.lang.NullPointerException3. ruby - jekyll serve 运行报错4. python-appium代码运行报错5. python - pycharm运行报错unable to load the file system codec ImportError ?6. python - 深入理解Flask 第三章代码运行报错, 代码完全正常,为什么报SQL语法错误?7. webpack - node.js 运行报错8. angular.js - angularjs项目使用grunt serve运行报错9. java - elasticsearch 运行报错10. node.js - 源码包运行nodejs运行报错