Overriding templates
If you have app and project templates directories that both contain overrides,the default Django template loader will try to load the template from theproject-level directory first. In other words, DIRS
is searched before .
参见
Read Overriding built-in widget templates if you're looking todo that.
Let's say you're trying to override the templates for a third-party applicationcalled blog
, which provides the templates blog/post.html
andblog/list.html
. The relevant settings for your project would look like:
The setting and will already exist if youcreated your project using the default project template. The setting that needsto be modified is DIRS
.
These settings assume you have a templates
directory in the root of yourproject. To override the templates for the blog
app, create a folderin the templates
directory, and add the template files to that folder:
Overriding from an app's template directory
Since you're overriding templates located outside of one of your project'sapps, it's more common to use the first method and put template overrides in aproject's templates folder. If you prefer, however, it's also possible to putthe overrides in an app's template directory.
First, make sure your template settings are checking inside app directories:
If you want to put the template overrides in an app called myapp
and thetemplates to override are named blog/list.html
and blog/post.html
,then your directory structure will look like: