私は最も簡単なことは、after_request
フックを使用することだと思います。
from flask import g
@main.route('/')
def index():
models = Model.query.all()
g.template = 'index'
return render_template('index.html', models=models)
@main.after_request
def store_template(response):
if hasattr(g, 'template'):
with open('debug/{0}-{1}'.format(datetime.now(), g.template), 'w') as f:
f.write(response.data)
return response
ここにドキュメントがあります。 http://flask.pocoo.org/snippets/53/
最後のn
テンプレートを収集する限り、私はおそらくそれを行うためにcronジョブをセットアップします。それが最新のファイルを保持しますので、ここで削除機能内のファイルの順番を逆に例
import os
from datetime import datetime
def make_files(n):
text = '''
<html>
</html>
'''
for a in range(n):
with open('debug/index-{0}.html'.format(datetime.now()), 'w') as f:
f.write(text)
def get_files(dir):
return [file for file in os.listdir(dir) if file.endswith('.html')]
def delete_files(dir, files, amount_kept):
rev = files[::-1]
for file in rev[amount_kept:]:
loc = dir + '/' + file
os.remove(loc)
if __name__ == '__main__':
make_files(7)
files = get_files('debug')
print files
delete_files('debug', files, 5)
files = get_files('debug')
print files
EDIT
です。また、ハードコードを避けるために元のテンプレート名にアクセスする方法を見つけることができません。
EDIT 2
は大丈夫そう
ドキュメントhttp://flask.pocoo.org/docs/0.11/testing/#faking-resources
あなたは
after_request
関数にテンプレート名を渡すためにflask.g
を使用する方法を示すために、それを更新いい質問、良い答え。私がお勧めする1つの変更は、Pythonのロギングを使用することです。ロギングの量を構成可能に制御でき、RotatingFileHandlerを使用できます。 https://docs.python.org/2/library/logging.handlers.html#rotatingfilehandler –テンプレート間でのみ問題があります。私はそれを試してみよう。 – Adam
ロガーを作成するときに、そのロガーに名前を付けると役立ちます。 –