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. ...
Get Django: Web Development with Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.