2017-08-09 11 views
1

djangoプロジェクトをPHPサーバに移動する必要があり、できるだけ多くのフロントエンドを保ちたい。 テンプレートをタグの付いていないHTMLファイルにレンダリングし、静的ファイルとメディアファイルの場合のように "template_root"に保存する簡単な方法はありますか?djangoテンプレートをhtmlファイルとしてレンダリングする

少なくとも、ビューのレンダリングはページの読み込みを行い、結果のHTMLをファイルに保存しますか? (ちょうどdevに関する!)

私はすべての「拡張」および「含んでいる」と「staticfiles」またはカスタムテンプレートタグ

答えて

1
を書き換えたくない、ビューからの動的なデータを懸念していませんよ

私は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を取得する方法がわかりません。それを再利用する機能にすることができます。クラスベースのミックスインとして簡単かもしれませんか?

関連する問題