Creating a Simple Template for Django
In Django we display web pages using templates. Let's see how we can display a full HTML page instead of the content we have given on the home page
Step 1
Create a template:-
To create a template for home page, first create a folder 'templates' in app folder.
Then create an html file called index.html in it and add some content required for the home page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Home page</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati error
iusto quos veritatis tenetur eveniet totam cum neque quas voluptatibus quod dicta,
pariatur exercitationem sit perferendis reprehenderit ea fugit voluptatum.</p>
</body>
</html>
Step 2
Make changes to the view:-
Make the necessary changes to the views to display the templates
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
def index(request):
template = loader.get_template("index.html")
return HttpResponse(template.render({},request))
Step 3
Add the app in Settings :-
Open settings.py under project folder and add your app name in installed apps list
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"app1",
]
Step 4
Run the project again and reload your home page to see the result
