を書き換えたくない、ビューからの動的なデータを懸念していませんよ
私はDjangoのrender_to_stringを使用して、ビューごとの拠点にこれを行う方法を思い付いた:
from django.template.loader import render_to_string
from django.views.generic import View
from django.shortcuts import render
from django.conf import settings
def homepage(request):
context = {}
template_name = "main/homepage.html"
if settings.DEBUG == True:
if "/" in template_name and template_name.endswith('.html'):
filename = template_name[(template_name.find("/")+1):len(template_name)-len(".html")] + "_flat.html"
elif template_name.endswith('.html'):
filename = template_name[:len(template_name)-len(".html")] + "_flat.html"
else:
raise ValueError("The template name could not be parsed or is in a subfolder")
#print(filename)
html_string = render_to_string(template_name, context)
#print(html_string)
filepath = "../templates_cdn/" + filename
print(filepath)
f = open(filepath, 'w+')
f.write(html_string)
f.close()
return render(request, template_name, context)
私はできるだけ一般的なようにそれを作ってみましたので、私は任意のビューに追加することができます。 すべてのテンプレートを繰り返し呼び出すビューを作成し、それらをすべて変換するビューを作成しました。 "collectstatic"に近いです。
render argsからtemplate_nameを取得する方法がわかりません。それを再利用する機能にすることができます。クラスベースのミックスインとして簡単かもしれませんか?