How to Add Django Website Templates in Django Project and ApplicationJune 2, 2021 2023-04-15 13:41
How to Add Django Website Templates in Django Project and Application
Django Website Templates in Django Project and Application
First of all, we answered the questions, What are Django website templates actually? It is the main component of an application that is related to the user frontend side. We designed templates for a website or web application using HTML and CSS. To create a website’s basic structure we use HTML and to design its structure, to look beautiful we CSS. In that template we upload website content, display form, render video, audio pictures, etc.
In Django, there is a facility, in which we make a folder in which all templates related files and folders are created. In this regard, we are learning how to add Django website templates in Django projects or applications. let’s start to learn!
We have published a complete Django guide, you can take that tutorial to learn its basics.
Why need Django Website Templates?
And you know, Django works with MVT architectural design patterns. In MVT, T is the template in which we show content to users in different styles. As every application or website needs a template. Basically, the template is the structure or layout with the design in which actual data is rendered. For this reason, we need to understand in Django, how we can add a template to upload actual data that will be displayed on the frontend.
Adding Django website template in Django Project
When we create a new Django project via a command, then by default we got different files and folders as urls.py, settings.py, and project folder, etc. So, we are learning, where we have to add a template folder in which our website’s template will be store. There is two work you have to do properly.
- First of all, you have to make a folder named ‘templates in the main directory of your project folder. In that folder, you can make another folder giving the name of the application. For example, you create a folder named ‘templates’ in that folder you will have to make a folder ‘User’ if you have an application named with User. Then in User, you will define template files like index.html, about.html, etc. After doing this, follow 2nd steps.
- In the settings.py file, you have to mention your template folder to tell Django, here is my template folder you have to use if you have wanted to use the template folder globally for all projects.
- After adding the template, then you can render the Django template easily in the views section. There are different ways to render templates in class-based views and function base views. You can get information from a linked web page.
Add templates location in settings.py file
TEMPLATES = [
Or you can use this code also for your project, both codes will work the same:
TEMPLATE_DIR = os.path.join(BASE_DIR,”templates”)
TEMPLATES = [
Adding Django website template in Django application
If you want to add Django website templates in the Django application then it is a very easy way to do it because you did not need to write code in this way. You will use this method if you want to keep the different templates for other applications or global project templates. you have to follow the following pattern if you want to us this way.
1. Django project > 2. Application folder > 3. Create a folder for template > 4. Give the same name as application name> 5. Your template files like index.html, about.html, etc
It is a five-step guideline for beginners that is the easiest way.
- At this point, you have to enter your Django main project folder in which you will find the manage.py file, in that location you will find your application folder for which you want to make a template folder separately.
- You have entered in your application folder like ‘User’ then you have to make a folder named “templates”. It is recommended to use the word “templates”.
- The next work is to make another folder for your application specifically. Keep in your mind, give folder name as your application name, it is recommended.
- In that application named folder, you have to define your template files like index.html, about.html, contact.html, etc
If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.
After reading today’s topic, if you face any problem while reading, understanding please discuss it with us. If you have understood and have any relevant questions then please discuss with us in the following comment section