LoginSignup
1
0

More than 3 years have passed since last update.

DjangoのTemplateDoesNotExistの対処法

Posted at

TemplateDoesNotExist

Djangoで、以下のコードを実行したときに上記のエラーが出た。

views.py

from django.template.response import TemplateResponse


def product_list(request):
    return TemplateResponse(request, 'catalogue/product_list.html')

どうすればいいのか。見るべき場所はsetting.pyの中の2箇所。

1.Templateのパス

以下のようになっているか確認する。

setting.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

こうなっていればオッケー。デフォルトでこうなっているはずなので、変更していなければ大丈夫なはず。

2.INSTALLED_APPS

筆者のエラーの原因はここだった。

setting.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'ec.catalogue',
]

INSTALLED_APPSには、そのプロジェクト内の全てのアプリケーションを登録する必要がある。
つまり、views.pyやurls.pyが入っている場所も追加しなければならない。
よって、アプリケーションが入っている場所(筆者の場合はec.catalogue)を追加することで、Templateを使えるようになった。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0