liveBook · Manning

Template directory

As explained in Django in Action

A template directory is a filesystem directory containing templates that a template engine can discover and load.

A template directory is a filesystem location where Django looks for HTML templates such as news.html. You can keep templates beside their apps or collect project HTML in one shared directory; reusable apps should ship their own templates. section 3.6

Configure and use a project-level template directory by:

  • Creating RiffMates/templates/news.html. section 3.6
  • Setting TEMPLATES['DIRS'] to [BASE_DIR / 'templates'] in settings.py. section 3.6
  • Returning render(request, "news.html", data) from the news view, then visiting /news/. section 3.6

RiffMates news page

Django finds news.html in the configured directory, renders its {% for %} loop with the news context, and produces a list of news items. section 3.6

Listing 3.7 The new templates directory and the soon-to-be-created news.html
RiffMates/
├── RiffMates
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── home
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
└── templates
    └── news.html

Use the project tree to locate the shared templates directory and its news.html file. section 3.6

How Django in Action covers it

How deep it goes
Explains directory organization, path configuration, rendering, and request flow without covering reusable-app implementation.
Examples
Uses RiffMates/templates/news.html, the news view, the /news/ route, and a loop over news items.
What you can do after
Operate/configure Django to locate and render a project template from a shared directory.
How it connects
Connects templates with Django views, settings.py, render(), context dictionaries, and reusable applications.

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.