Flask-Babel 调用 format_datetime 出现 RuntimeError: Working outside of application context. 异常

flask-babel

#1
>>> from flask_babel import format_datetime
>>> from datetime import datetime
>>> format_datetime(datetime(1987, 3, 5, 17, 12))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask_babel\__init__.py", line 397, in format_datetime
    format = _get_format('datetime', format)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask_babel\__init__.py", line 350, in _get_format
    babel = current_app.extensions['babel']
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\werkzeug\local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\werkzeug\local.py", line 306, in _get_current_object
    return self.__local()
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\globals.py", line 51, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.
>>>

#2

Flask-Babel 提供的多个函数依赖于 Flask 程序上下文,需要使用 flask shell 命令启动 Python shell。下次贴命令行请同时给出执行的命令等信息。另外,建议发帖时在标题中添加必要的关键信息,并为帖子选择合适的分类。


#3
(FlaskProjects-BArbVR56) D:\FlaskProjects\FlaskHTTP\myalbumy>flask shell
Python 3.7.1rc2 (v3.7.1rc2:6c06ef7dc3, Oct 13 2018, 15:44:37) [MSC v.1914 64 bit (AMD64)] on win32
App: myalbumy [development]
Instance: D:\FlaskProjects\FlaskHTTP\myalbumy\instance
>>> from flask_babel import format_datetime
>>> from datetime import datetime
>>> format_datetime(datetime(1987,3,5,17,15))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "c:\users\张启宏\.virtualenvs\flaskprojects-barbvr56\lib\site-packages\flask_babel\__init__.py", line 397, in format_datetime
    format = _get_format('datetime', format)
  File "c:\users\张启宏\.virtualenvs\flaskprojects-barbvr56\lib\site-packages\flask_babel\__init__.py", line 350, in _get_format
    babel = current_app.extensions['babel']
KeyError: 'babel'
>>>

好的,可出现了以上错误了?


#4

意思就是说要这样:

app = Flask(__name__)
with app.app_context():
    # TODO:

不然就会报下面错误:

RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.

#5

突然想起 flask shell 命令是帮忙执行 with app.app_context(): 了…:upside_down_face::upside_down_face::upside_down_face::rofl:

babel = current_app.extensions['babel']
KeyError: 'babel'

表示你没有 babel.init_app(app) 或者 babel=Babel(app)

参见书中【15.3 编写扩展类】小节【代码清单15-2】。


#6

指点正确,谢谢!

>>> from flask_babel import Babel
>>> from flask_babel import format_datetime
>>> from datetime import datetime
>>> babel=Babel()
>>> babel.init_app(app)
>>> format_datetime(datetime(1987,3,5,17,50))
'Mar 5, 1987, 5:50:00 PM'
>>>