In the previous article, we talked about the views in which we are returning the HTTP response. Now let's see how to render an HTML file as a response.
First of all, you have to change setting.py
. In TEMPLATES
array add html folder path in DIRS
option.
# myproject/setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['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',
],
},
},
]
Now, for creating templates for your project follow the steps given below.
- create a templates directory in your project
The first step is to create a directory as you described in settings.py
for storing HTML files. In my case, I will create a directory called "templates" at the root of your project.
- Create a template file
Inside the templates directory, create a new file with a name that corresponds to the view that you want to use the template with. For example, if you have a view that displays a home page of your website, you might create a file called "home.html".
- Define a template
In the template file, you will define the structure of the web page. This can include HTML tags, variables, and control structures such as loops and conditions. you can read about it in the official documentation. write some random HTML in the home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<h1>{{ msg }}</h1>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
</body>
</html>
- Use the template in a view
In the view that corresponds to the template, use the render()
function to render the template and return it to the browser. The render()
function takes two arguments: the request object and the name of the template. you can give 3rd argument as context
which contains a bunch of key-value pair which can be accessed in html file. For example, to render the "home.html" template, you would use the following code:
# home/views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'home.html',{msg:"Hello World"})
- Include the template in the URL
Finally, you will need to include the template in the URL. To do this, you will need to add a URL pattern that maps the URL to the view. For example, you might use the following code.
# home/urls.py
from django.urls import path
from home import views
urlpatterns = [
path('', views.index, name='index'),
]
That's it! You should now have a basic understanding of how to use templates in Django. Remember that you can use templates to create dynamic web pages that can be easily reused across different views and applications. With the knowledge of how to use templates in Django, you can create more complex and powerful web applications.
Thanks for reading 🤩 and follow me on Twitter ❤️.