2

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',
            ],
        },
    },
]
inthenameofmusik
  • 617
  • 1
  • 4
  • 16

2 Answers2

4

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Thank you for the answer, and the additional recommendation against putting `testing` app in `INSTALLED_APPS`. You recommend keeping the 'root' (`testing`) app out of there because it should only be used as a type of router to the correct app? – inthenameofmusik Jan 02 '16 at 22:09
  • 1
    The directory containing `settings.py` and the root url conf isn't usually included in `INSTALLED_APPS`. As you say, the root url conf usually includes urls from other apps. I have seen some people include it in `INSTALLED_APPS` before, so I'm not saying you absolutely shouldn't do it. – Alasdair Jan 02 '16 at 22:17
3

By specifying "APP_DIRS": True, you are telling django to search for template files inside each app installed. Check in settings.py whether all your apps are contained within INSTALLED_APPS. If they are, you can try to force django to look for the templates in your app.

TEMPLATES = [
    {
        'DIRS': [os.path.join(BASE_DIR,  'testing', 'templates')],
        ....
    },
]
Mirac7
  • 1,566
  • 4
  • 26
  • 44