pythondjangodjango-urlsurlconf

Django TemplateView searching in wrong place


My Django structure is:

testing
    contacts
        __init__.py
        templates
            contacts
                index.html
        (additional modules)
    testing
        __init__.py
        templates
            testing
                test.html
        urls.py
        (additional modules)

Inside of the main URL module, testing.urls, I have a urlconf that is as follows:

url(r'^testing/$', TemplateView.as_view(template_name='testing/test.html'))

The problem is, it keeps looking in contacts.templates.contacts for the test.html file. With the existing urlcon, the debug page says the following:

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Library/Python/2.7/site-packages/django/contrib/admin/templates/testing/test.html (File does not exist)
/Library/Python/2.7/site-packages/django/contrib/auth/templates/testing/test.html (File does not exist)
/Users/*/Developer/django/testing/contacts/templates/testing/test.html (File does not exist)

It always defaults to the..........................^^^^^^^^^^ contacts folder for some reason. Is there some other parameters for TemplateView or template_name that can control this? Any help appreciated!


Update - Inside 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',
            ],
        },
    },
]

Solution

  • The testing/testing directory is not currently being searched by Django. The easiest fix is to add it to the DIRS setting:

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

    Another option would be to add testing to your INSTALLED_APPS setting. Then Django would find your template, since you have APP_DIRS=True. However I wouldn't recommend this, because in your case testing/testing is the special directory that contains the settings.py and root url config.