1
私がHerokuでホストしているdjangoアプリケーションでは、LaTeXテンプレートからPDFを生成し、それを一時ファイルとして保存するビューがあります。表示は次のとおりです。Heroku Django ApplicationError: 'module'に 'TemporaryDirectory'属性がありません
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from subprocess import Popen, PIPE
import tempfile
import os
def pdf(request):
context = Context({})
template = get_template('cv/simple.tex')
rendered_tpl = template.render(context).encode('utf-8')
with tempfile.TemporaryDirectory() as tempdir: ## ERROR RAISED HERE ##
process = Popen(
['pdflatex', '-output-directory', tempdir],
stdin=PIPE,
stdout=PIPE,
)
process.communicate(rendered_tpl)
with open(os.path.join(tempdir, 'texput.pdf'), 'rb') as f:
pdf = f.read()
r = HttpResponse(content_type='application/pdf')
r.write(pdf)
return r
これはローカルで正常に動作します。ビューで指摘したときに、私はHerokuのにプッシュしてURLを訪問してみてくださいしかし、私は次のエラーを取得する:同様のエラーのため
Internal Server Error: /cv.pdf
Traceback (most recent call last):
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/cv/views.py", line 34, in pdf
with tempfile.TemporaryDirectory() as tempdir:
AttributeError: 'module' object has no attribute 'TemporaryDirectory'
その他の質問には、スクリプトの呼び出しtempfile.py
を有するものが原因であることを示唆していますそれはPythonライブラリの代わりにインポートされますが、私はそれを持っていません(Herokuがしない限り)。助言がありますか?
ご協力いただきありがとうございます。