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.
- Django Templates | Set - 2
- Django Forms
- How to Create an App in Django ?
- Django Models | Set - 1
- Django Models | Set - 2
- Django ModelFormSets
- Django Formsets
- Django Models
- How to use Django Field Choices ?
- BooleanField - Django Forms
- URLField - Django Models
- DurationField - Django Models
- EmailField - Django Models
- CharField - Django Forms
- IntegerField - Django Forms
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.