In Django , templates are crucial for creating the static parts of web pages. They serve as the foundation or skeleton on which the dynamic content is added. Templates make Django follow the MVT (Model-View-Template) architecture: models handle data, views handle user interactions, and templates handle the layout and structure of web pages.
Simply put, templates in Django are like blueprints for web pages. They define how the page should look and what static elements it should contain. When a user visits a web page, Django uses views to render these templates and fill in any dynamic content before sending the complete webpage to the user's browser.
Create a Template Folder
Create a folder named
templates
in your Django application directory if it doesn't already exist. This folder will contain all your HTML template files.Specify the Template Folder Location in settings.py
Open your
settings.py
file located in your Django projects' directory. Find theTEMPLATES
variable and ensure that theDIRS
option includes the path to your templates folder. Example:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
}
]
Place the HTML File Inside the Templates Folder
Create or move your HTML template file into the
templates
folder.Example: Create a file named
about.html
andhomepage.html
inside thetemplates
folder.Edit the Views in views.py File
After ensuring that the HTML files are in place, you'll edit the
views.py
file to define the view functions for rendering these HTML templates. This code snippet is from a Djangoviews.py
file and contains two view functions:homepage
andabout
.from django.http import HttpResponse from django.shortcuts import render def homepage(request): return render(request, 'homepage.html') def about(request): return render(request, 'about.html')
Homepage Function: This function handles requests to the homepage of a website. Inside the function, it uses the
render()
function fromdjango.shortcuts
to render an HTML template named'homepage.html'
.
Therender()
function takes therequest
object and the name of the template file as arguments and returns an HTTP response with the rendered template. In this case, the function returns the renderedhomepage.html
template as the HTTP response.About Function: This function handles requests to the about page of the website. Similar to the
homepage
function, it takes arequest
object as its parameter.
In summary, these view functions render HTML templates (homepage.html
and about.html
) and return them as responses to requests for the homepage and about page of the website, respectively.
Include the View in the urls.py File
Open your
urls.py
file located in your Django application directory. The code snippet is from a Djangourls.py
file and contains URL patterns mapped to view functions using thepath()
function.
from django.urls import path
from django.contrib import admin
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('about/', views.about, name='about'),
path('', views.homepage, name='homepage'),
]
About Page URL:
path('about/', views.about, name='about')
: This URL pattern maps requests to the'about/'
URL to theabout
view function defined in theviews.py
file. Thename='about'
parameter provides a name for this URL pattern, which can be used to reference it in templates or view functions.Homepage URL:
path('', views.homepage, name='homepage')
: This URL pattern maps requests to the root URL (''
) to thehomepage
view function defined in theviews.py
file. Thename='homepage'
parameter assigns the name 'homepage' to this URL pattern for easy reference.
Reload the Server
Open a terminal or command prompt and navigate to your Django projects' directory. Run the command
python manage.py runserver
to start the development server. Open a web browser, navigate to the specified URL, and verify that the webpages (homepage.html
andabout.html
) are rendered correctly.
By following these steps, you should be able to render HTML templates in your Django application and view them in your web browser.