Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Django Templates Templates

In 1.9 I had to set the DIRS property in TEMPLATES to '/home/www/treehouse/templates',

Less of a question and more of an update. 1.9 requires defining the whole tree for the template directory - at least for me on Debian.

7 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Setting the TEMPLATES DIRS certainly works. As an alternative, the default settings.py file has APP_DIRS set to True which indicates to automatically check for templates in the app directoiries such as learning_site/courses/templates/courses if present. if not set, APP_DIRS defaults to False

If you were to create this directory and move your templates there, you would not need to change the settings.py file.

# settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

As Matthew mentioned, the app dirs aren't a good place for the root templates.

An alternative to hardcoded static paths is to use the BASE_DIR in the settings file:

        'DIRS': [os.path.join(settings.BASE_DIR, 'templates'), ],

Tagging Kenneth Love to update Teacher's Notes to information on setting TEMPLATES, DIRS correctly for non-app templates.

Gavin Ralston
Gavin Ralston
28,770 Points

In 1.9.1 this worked for me:

  • 'templates' was not in the DIRS list, had to add it manually,
  • DIRS was already set to True. As noted, this is necessary to have individual apps searched for templates
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates',],    
        'APP_DIRS': True,          
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Hi Chris, thanks for your reply.

That definitely works if you don't mind having your index/main page templates in the courses app template namespace.

However, he instructs us to create a templates dir in the /root_project/templates/ to keep it outside of the courses app:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates',],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

In the example above, the 'DIRS' directory doesn't work for me unless i set the property to the full path.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Good point Matthew. I've updated my answer and tagged Kenneth to update the Teacher's Notes.

Thank you, Chris :)

Hamid Salehi
Hamid Salehi
5,721 Points

How do I find my BASE_DIR in os.path.join(settings.BASE_DIR, 'templates') ? I am using AWS

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

In your project settings.py file, BASE_DIR can be defined using:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

This will set the value relative to the location of the settings.py file. You can nest multiple os.path.dirname() calls to work up to any parent directory, as desired. In the code above, BASE_DIR is set to the parent directory of the directory containing the settings.py file.

Hamid Salehi
Hamid Salehi
5,721 Points

that is how my BASE_DIR is setup. The problem is when I add os.path.join(settings.BASE_DIR, 'templates'), to DIRS and run, I get this server error.

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at you@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Can you access the logs to see what the specific error is?

What file contains the code that causes the raised error? if it is also in settings.py then remove "settings." from statement.

If code is in another file, you'll need to access it by importing the settings object:

from django.conf import settings

More in django docs

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

You can also set DEBUG = True in settings.py to see the error directly. Not recommended for production deployments.

Hamid Salehi
Hamid Salehi
5,721 Points

here are the logs:

[Thu Dec 08 07:08:22.152794 2016] [:error] [pid 3183] [remote 169.234.228.68:0] mod_wsgi (pid=3183): Target WSGI script '/opt/bitnami/apps/django/django_projects/Project/Project/wsgi.py' cannot be loaded as Python module. [Thu Dec 08 07:08:22.152821 2016] [:error] [pid 3183] [remote 169.234.228.68:0] mod_wsgi (pid=3183): Exception occurred processing WSGI script '/opt/bitnami/apps/django/django_projects/Project/Project/wsgi.py'. [Thu Dec 08 07:08:22.152842 2016] [:error] [pid 3183] [remote 169.234.228.68:0] Traceback (most recent call last): [Thu Dec 08 07:08:22.152860 2016] [:error] [pid 3183] [remote 169.234.228.68:0] File "/opt/bitnami/apps/django/django_projects/Project/Project/wsgi.py", line 19, in <module> [Thu Dec 08 07:08:22.152899 2016] [:error] [pid 3183] [remote 169.234.228.68:0] application = get_wsgi_application() [Thu Dec 08 07:08:22.152908 2016] [:error] [pid 3183] [remote 169.234.228.68:0] File "/opt/bitnami/apps/django/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/core/wsgi.py", line 13, in get_wsgi_application [Thu Dec 08 07:08:22.152924 2016] [:error] [pid 3183] [remote 169.234.228.68:0] django.setup(set_prefix=False) [Thu Dec 08 07:08:22.152931 2016] [:error] [pid 3183] [remote 169.234.228.68:0] File "/opt/bitnami/apps/django/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/init.py", line 22, in setup [Thu Dec 08 07:08:22.152941 2016] [:error] [pid 3183] [remote 169.234.228.68:0] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) [Thu Dec 08 07:08:22.152949 2016] [:error] [pid 3183] [remote 169.234.228.68:0] File "/opt/bitnami/apps/django/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/conf/init.py", line 53, in getattr [Thu Dec 08 07:08:22.152960 2016] [:error] [pid 3183] [remote 169.234.228.68:0] self.setup(name) [Thu Dec 08 07:08:22.152967 2016] [:error] [pid 3183] [remote 169.234.228.68:0] File "/opt/bitnami/apps/django/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/conf/init.py", line 41, in _setup [Thu Dec 08 07:08:22.152977 2016] [:error] [pid 3183] [remote 169.234.228.68:0] self._wrapped = Settings(settings_module) [Thu Dec 08 07:08:22.152983 2016] [:error] [pid 3183] [remote 169.234.228.68:0] File "/opt/bitnami/apps/django/lib/python2.7/site-packages/Django-1.10.3-py2.7.egg/django/conf/init.py", line 97, in __init_ [Thu Dec 08 07:08:22.152994 2016] [:error] [pid 3183] [remote 169.234.228.68:0] mod = importlib.import_module(self.SETTINGS_MODULE) [Thu Dec 08 07:08:22.153000 2016] [:error] [pid 3183] [remote 169.234.228.68:0] File "/opt/bitnami/python/lib/python2.7/importlib/init.py", line 37, in import_module [Thu Dec 08 07:08:22.153011 2016] [:error] [pid 3183] [remote 169.234.228.68:0] import(name) [Thu Dec 08 07:08:22.153017 2016] [:error] [pid 3183] [remote 169.234.228.68:0] File "/opt/bitnami/apps/django/django_projects/Project/Project/settings.py", line 59, in <module> [Thu Dec 08 07:08:22.153039 2016] [:error] [pid 3183] [remote 169.234.228.68:0] 'DIRS': [os.path.join(settings.BASE_DIR, 'templates'), ], [Thu Dec 08 07:08:22.153054 2016] [:error] [pid 3183] [remote 169.234.228.68:0] NameError: name 'settings' is not defined

Hamid Salehi
Hamid Salehi
5,721 Points

remove "settings" from os.path.join(settings.BASE_DIR, 'templates') solved the issue! Thanks a lot Chris.