user7410898
user7410898

Reputation: 119

Django not searching templates directory defined in APP_DIRS setings

I got a templates directory in same level as my project directory so I put in settings.py

'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,

but Django is not searching templates in apps it stops at DIRS

all apps needed are installed

Upvotes: 6

Views: 5101

Answers (5)

Michele
Michele

Reputation: 101

I was attempting to override templates from a third-party package.

My issue was that I had INSTALLED_APPS in the following order:

INSTALLED_APPS = [
    # core
    # third-party
    # mine
]

Because I clearly glossed over the documentation which tells you this is not how it works: https://docs.djangoproject.com/en/3.1/ref/settings/#installed-apps

Specifically the last line:

When several applications provide different versions of the same resource (template, static file, management command, translation), the application listed first in INSTALLED_APPS has precedence.

Treat it like fallback order and ensure the overrides are before the originals, et voila. Sigh.

Upvotes: 0

rajiv.cla
rajiv.cla

Reputation: 366

My solution was to put the app name at the end of INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'posts'  # <= put your app name here i guess?
]

Upvotes: 9

amchugh89
amchugh89

Reputation: 1296

I got this today (I'm on django 2.1). Solution was to add 'myappname' to the INSTALLED_APPS in settings (something you used to have to do on older django versions, but I thought was not mandatory anymore). Without doing this, django template processors were not looking to myappname/templates/myappname/

I also have the TEMPLATES_DIRS set up like some other answers, but for whatever reason that was not sufficient on my current setup (I broke my laptop, so I am django-ing on windows w brand new python 3.7 and brand new django 2.1 (didn't have to do this step the last time I worked w/ django about a week ago...)

Upvotes: 2

vasi1y
vasi1y

Reputation: 815

Check your DIRS setting. It should include templates directory itself.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],

        # True or False, depends on your case.
        'APP_DIRS': True,

        # Default django setup.
        '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',
            ],
        },
    },
]

Upvotes: 1

l_degaray
l_degaray

Reputation: 63

Just put the name of the template directory under the TEMPLATES array in the 'DIRS' section in your settings.py

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

Upvotes: 0

Related Questions