Django Templates | Set – 1
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.
- We can use a single template directory which will be spread over the entire project.
- 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
In above code, modify
'DIRS':  as '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.