Overriding templates¶
プロジェクトでは、django.contrib.admin
のような contrib アプリケーションやサードパーティアプリケーションで Djangoアプリケーションのテンプレートを上書きすることができます。 テンプレートの上書きは、プロジェクトのテンプレートディレクトリまたはアプリケーションのテンプレートディレクトリに置くことができます。
オーバーライドが含まれている app と project の両方のテンプレートディレクトリがある場合、デフォルトの Django テンプレートローダは、まずプロジェクトレベルのディレクトリからテンプレートをロードしようとします。つまり、 DIRS
は APP_DIRS
より前に検索されます。
参考
そのようなことをしたい場合は、Overriding built-in widget templates を読んでください。
Overriding from the project's templates directory¶
最初に、プロジェクトのテンプレートディレクトリに置き換え用テンプレートを作成して、テンプレートのオーバーライドを検討します。
Let's say you're trying to override the templates for a third-party application
called blog
, which provides the templates blog/post.html
and
blog/list.html
. The relevant settings for your project would look like:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
...,
'blog',
...,
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
...
},
]
The TEMPLATES
setting and BASE_DIR
will already exist if you
created your project using the default project template. The setting that needs
to be modified is DIRS
.
These settings assume you have a templates
directory in the root of your
project. To override the templates for the blog
app, create a folder
in the templates
directory, and add the template files to that folder:
templates/
blog/
list.html
post.html
The template loader first looks for templates in the DIRS
directory. When
the views in the blog
app ask for the blog/post.html
and
blog/list.html
templates, the loader will return the files you just created.
Overriding from an app's template directory¶
Since you're overriding templates located outside of one of your project's apps, it's more common to use the first method and put template overrides in a project's templates folder. If you prefer, however, it's also possible to put the overrides in an app's template directory.
First, make sure your template settings are checking inside app directories:
TEMPLATES = [
{
...,
'APP_DIRS': True,
...
},
]
If you want to put the template overrides in an app called myapp
and the
templates to override are named blog/list.html
and blog/post.html
,
then your directory structure will look like:
myapp/
templates/
blog/
list.html
post.html
With APP_DIRS
set to True
, the template
loader will look in the app's templates directory and find the templates.