KB303
KB303

Reputation: 143

Django - templatedoesnotexist

I'm creating a project named "crepes_bretonnes". In this project, I have an application blog. I created a template date.html. Here is the structure of my folders :

crepes_bretonnes/
    blog/
        __init__.py
        admin.py
        migrations/
            __init__.py
        models.py
        templates/
            blog/
                addition.html
                date.html
        tests.py
        views.py
    crepes_bretonnes/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    templates/
    db.sqlite3
    manage.py

When I try to see the page, I have a message templateDoesNotExist. I have read a lot about it on the web but I have not succeed in resolving my problem. In fact, I don't understand why Django does not search in my template folder of the app blog although I wrote "blog" in INSTALLED_APP in setting.py. Obviously, I have put TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),) in setting.py. I also tried to change the dictionary TEMPLATES. However, if I have understood well, it has no link as here Django should find my template even without this. I don't have any solution.

Thank you for your help.

PS: If I put date.html in the general template folder and I arrange some lines, it works. However that is not a solution, I would like to respect a good structure.

UPDATE:

Thank you for your answer. Yes it really says INSTALLED_APPS in my setting and APP_DIRS is already True.

Here is my TEMPLATES in setting.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "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',
            ],
        },
    },
]

UPDATE: The debug message shows that Django search in django.template.loaders.app_directories.Loader: /Users/benjamin/anaconda/lib/python2.7/site-packages/django/contrib/admin/templates/blog/date.html. However I have not written anything in these folder ... I'm working in Documents. Why does Django search here and not in Documents ?

Upvotes: 3

Views: 8072

Answers (6)

aviv avital
aviv avital

Reputation: 1

For me, it was using rest_framework without adding it to INSTALLED_APPS.

Upvotes: 0

Taregh Naderi
Taregh Naderi

Reputation: 259

I solved it by adding forward slash "/" after 'templates':

'DIRS': [BASE_DIR / 'templates/'],

Upvotes: 2

mohaa8844
mohaa8844

Reputation: 441

me having the same issue, added the app to the INSTALLED_APPS done, because you (and me) were using the templates not in the main app so it must be registered in the settings (like referring to the urls of the app from the main urls file)

Upvotes: 1

Gabriel
Gabriel

Reputation: 171

Go to your setting.py and try something like:

os.path.join(BASE_DIR, "templates")

For example:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, "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',
        ],
    },
},

]

Upvotes: 0

yogesh
yogesh

Reputation: 26

I had the same problem saying template does not exist and I solved it by changing the URL pattern in my views.py where previously I had mentioned just date.html, later I edited with blog/date.html. Hope it might help you.

Upvotes: 1

user2390182
user2390182

Reputation: 73460

I assume 'INSTALLED_APP' is a typo and it really says 'INSTALLED_APPS' in your settings. Anyway, you need to set APP_DIRS = True for templates to be found in app/templates folders:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
                 os.path.join(BASE_DIR, 'templates'),
                 ],
        'APP_DIRS': True,  # this line is important
        '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

Related Questions