Organizing templates
The default project layout created by the startproject
command does not define a location for your templates. This is very easy to fix. Create a directory named templates
in your project's root directory. Add the TEMPLATE_DIRS
variable in your settings.py
:
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
That's all. For example, you can add a template called about.html
and refer to it in the urls.py
file as follows:
urlpatterns = patterns( '', url(r'^about/$', TemplateView.as_view(template_name='about.html'), name='about'),
Your templates can also reside within your apps. Creating a templates
directory inside your app
directory is ideal to store your app-specific templates.
Here are some good practices to organize your templates:
- Keep all app-specific templates inside the
app
's template directory within a separate directory, for example...