Python Django “Template does not exist” error

In this Python Django tutorial we are going to discuss the most common Django error we get when we build an application i.e. Python Django “Template does not exist at app_name“.

We will learn to fix this error as well as we will try to understand the reasons for its occurrence so that we can avoid those mistakes in our future projects.

Template Does Not Exist Error
DjangoTemplate Does Not Exist Error

Error: “Template does not exist”

We generally get this Python Django error due to the following reasons not registering the app, using the wrong app name, not defining the templates in the ‘TEMPLATES’ in the settings.py file, and not defining the path for URLs. We will discuss these errors in further points.

1. Not defined App in Settings.py

In the Settings.py file go to the INSTALLED APP section and see if you have registered your application there or not. If you have not defined it, define it first.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app_name',
]

2. Templates Settings

For the Template, settings go to the Settings.py file and there you will find a section ‘TEMPLATES’ There check if APP_DIR is “True” or not, and if it will be “False ” Django will not look for Templates in the App. In ‘ DIR ‘ make sure have defined the path of the templates as mentioned in the below code.

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

3. Template path Structure.

Templates are defined on the application level, where we create a separate folder for the Templates which consists of HTML files that are Templates we render in our application. Here we need to take of proper structure we create for the Template files.

If the structure of the template is like the structure mentioned below then the path in the DIR should be templates/app_name.

app_name
|
|-templates
|    |-app_name
|    |    |-index.html

In case the structure of the Template is like the structure mentioned below then the path in DIR should be ‘DIRS’: [ ‘templates‘ ],

app_name
|
|-templates
    |-index.html

Read Django IntegrityError at /modelname/

4. Templates on the Project level

Templates can be created on the Project level also where we create Templates outside the application. Many times we follow this structure where the Templates are created in the Project directory instead of the App directory. For reference Project level Template structure is shown below.

|-project_name
|    |-templates
|    |    |-project_name
|    |    |    |-template-name

While using the above structure we need to define the DIR path in a different way than we use to define in the application-based DIR, for that go to the settings.py file, and in the DIR section follow the code mentioned below.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Conclusion:

In the above-mentioned steps, we have discussed the possible reasons that we face in Python Django projects in terms of templates.

In this Python Django tutorial, we also learned about the occurrence of the Django “Template does not exist” error and discussed the methods to fix this error through the methods we have discussed in this tutorial.

You may also like to read the following articles: