私はこれをいくつかの方法で試しましたが、うまくいきませんでした。ビューを次のようにレンダリングしようとすると:
from django.shortcuts import render
from django.template import loader
def index(request):
render(request, loader.get_template('index.html'))
次のエラーが表示されます。
TemplateDoesNotExist at /
コードを次のように変更すると:
from django.http import HttpResponse
from django.template.loader import render_to_string
def index(request):
content = render_to_string('index.html')
HttpResponse(content)
実際にテンプレートを見つけてレンダリングします(content
レンダリングされたhtmlに設定されます)が、次のエラーが発生します:
ValueError at /
The view home.controller.index didn't return an HttpResponse object.
これが私のフォルダー構造と私の設定です:
myProject/
settings.py
home/
controller.py
urls.py
models.py
templates/
home/
index.html
私のsetting.pyファイルの中に私は持っています:
SITE_ROOT = os.path.dirname(os.path.realpath(__name__))
TEMPLATE_DIRS = ( os.path.join(SITE_ROOT, 'home/templates/home'), )
INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.staticfiles',
'gunicorn',
'home'
)
の複数のバリエーションを試しましたが、思ったアプリとして追加したTEMPLATE_DIRS
ので、正しく拾うだけだとhome
思います。ここで何が起こっているか知っている人はいますか?
アップデート
物事の組み合わせがこれを修正しました。まず、return
ステートメントが必要です ( doh )。テンプレートをレンダリングする方法の例を混ぜていたと思います。ローダーをインポートしたり、手動でレンダリングしたりする必要はありません。これは私が実際に働いたものです:
from django.shortcuts import render
def index(request):
return render(request, 'home/index.html')
正しい方向に向けてくれた@laloと@Raoに感謝します。