Find file in templates directory

Asked

Viewed 56 times

1

In versions up to 2.2.5 of Django I put my html files that were passed in the view as follows :

myapp
   templates
       myapp
          index.html

But in Django 2.2.6 and 2.2.7 when I do this gives the following error :

TemplateDoesNotExist at /

But when I do :

myapp
   templates
       index.html

The template is found.

As in these newer versions Django makes the treatment to identify 2 templates with the same name without needing the subdirectory to distinguish ?

  • Beto, didn’t you put the most important in your question... how are you calling this template? Because in its first folder structure just call myapp/index.html that everything would be OK...

1 answer

0


I believe it is a configuration in the file settings.py of the project.

Check if you have the TEMPLATES variable, like this:

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

Important:

'DIRS': ['templates']: Define the name of the folders where Django will look for the templates, in your case, is templates.

'APP_DIRS': True: Tells you if Django should look for templates within the applications, in your case it’s True.

By doing this setup it will find templates inside the 'templates' folder inside your application.

Something else:

If your structure is:

myapp
   templates
       myapp
          index.html

The path of your template is not index.html and yes myapp/index.html

I hope it helps you. Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.