Open In App
Related Articles

Django Templates | Set – 1

Improve Article
Save Article
Like Article

There are two types of web pages – Static and Dynamic pages. Static webpages are those pages whose content is static i.e. they don’t change with time. Every time you open that page, you see the same content. Their content is independent of time, location, user, etc. Dynamic webpages are those pages whose content are generated dynamically i.e. They vary as per location, time, user and on various factors.

What are templates ?

Django framework efficiently handles and generates dynamically HTML web pages which are visible to end-user. Django mainly functions with backend so, in order to provide frontend and provide a layout to our website, we use templates. There are two methods of adding the template to our website depending on our need.

  1. We can use a single template directory which will be spread over the entire project.
  2. For each app of our project, we can create a different template directory.

For our current project, we will create a single template directory which will be spread over the entire project for simplicity. App-level templates are generally used in big projects or in case we want to provide a different layout to each component of our webpage.

Adding template to project –

Create a template directory in the same directory as our project. In our case, which is geeksforgeeks. So, our directory structure is now,
Now, navigate to geeksforgeeks/geeks_site/

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

In above code, modify

'DIRS': []

'DIRS': [os.path.join(BASE_DIR, 'templates')]

Above line links our project to our template directory using the os module. If you print BASE_DIR in your terminal, you will see the directory of your project. For example in my case, it is


Now our command will join our BASE_DIR to ‘templates’ and feed it to ‘DIRS’ key of TEMPLATE. Now, we can our save HTML codes in geeksforgeeks/templates directory and can access it from our code.

Last Updated : 26 Aug, 2019
Like Article
Save Article
Similar Reads
Related Tutorials