の理由についてはMartinの答えがとなっています。
問題を提起した回答が解決しましたが、確かに唯一の方法ではありません。私の場合は、より多くのようなものだった:
import threading
from flask import Flask, render_template
app = Flask("myapp")
app.route('/')
def get_thing(thing_id):
thing = cache.get(thing_id)
if thing is None:
# Handle cache miss...
elif is_old(thing):
# We'll serve the stale content but let's
# update the cache in a background thread
t = threading.Thread(
target=get_thing_from_datastore_render_and_cache_it,
args=(thing_id,)
)
t.start()
return thing
def get_thing_from_datastore_render_and_cache_it(thing_id):
thing = datastore.get(thing_id)
cache.set(render_template(thing))
をしかしget_thing_from_datastore_render_and_cache_it
が、私は上記のようなエラーを得ていたフラスコ要求サイクル外のバックグラウンドスレッドで実行されたとき、そのスレッドは、要求コンテキストへのアクセスを持っていなかったので。
Flaskは、Jinja2自身の機能ではなく、Jinja2の機能を包み込む方法に関するFlaskの決定によって自動的にテンプレート内の要求変数にアクセスするための開発者向けのショートカットを提供するため、エラーが発生します。
import jinja2
def render_without_request(template_name, **template_vars):
"""
Usage is the same as flask.render_template:
render_without_request('my_template.html', var1='foo', var2='bar')
"""
env = jinja2.Environment(
loader=jinja2.PackageLoader('name.ofmy.package','templates')
)
template = env.get_template(template_name)
return template.render(**template_vars)
この関数は、あなたのフラスコのアプリは、伝統的なテンプレートサブフォルダを持っていることを前提としています。これを解決するための私のアプローチはJinja2の者が直接レンダリングを使用するだけでした。あなたはtemplates/
の下にサブディレクトリ構造を持っている場合は具体的には、ここではプロジェクトの構造が
.
└── name/
├── ofmy/
| ├── package/
| | ├── __init__.py <--- Where your Flask application object is defined
| | └── templates/
| | └── my_template.html
| └── __init__.py
└── __init__.py
だろう、あなただけのテンプレートのルートからの相対パスを渡すあなたがフラスコのrender_template
を使用する場合と同様に同じフォルダ。
本当にすばらしい説明。私はフラスコの外で仕事をしていたので、私はそれがわからなかった。 –
私は、パッケージの名前をどうやって知っていますか? –