Exploring Django: Rendering HTML Templates
Crafting Dynamic Web Pages with Django's Template System

I am a versatile developer skilled in web development and data science. My expertise includes HTML, CSS, JavaScript, TypeScript, React, Python, Django, Machine Learning, and Data Analysis. I am currently expanding my knowledge in Java and its applications in software development and data science. My diverse skill set enables me to create dynamic web applications and extract meaningful insights from data, driving innovative solutions.
Introduction
Welcome back to our Django blog series!
In this blog, we'll delve into the process of rendering HTML templates using Django's powerful template system.
We'll firstly walk through creating a path in the urls.py file, defining a view function in views.py, and returning an HTTP response containing a simple message.
Create a Path in
urls.py: Open theurls.pyfile in your Django project's main folder (myprojectin our example). Add a new path that maps to a view function responsible for rendering theindex.htmltemplate.# myproject/urls.py from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]Define a View Function: In the
views.pyfile within your Django application folder, importdjango.http.HttpResponseand define a view function namedindexthat returns an HTTP response containing the rendered HTML template.# myapp/views.py from django.http import HttpResponse def index(request): return HttpResponse("Hello there! Welcome to our Django blog series.")Run the Development Server: Start the Django development server using the
manage.pyscript.python manage.py runserverRendering HTML Templates
Django's template system allows developers to create dynamic and reusable HTML templates, separating the presentation layer from the business logic of the application. Let's begin by creating a simple HTML template and rendering it using Django.
Create an HTML Template: Within your Django project, create a folder named
templatesin the root directory. Inside this folder, create a new HTML file namedindex.htmlwith the following content:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hello Django!</title> </head> <body> <h1>Hello Django!</h1> <p>Welcome to our Django blog series.</p> </body> </html>Create a Path in
urls.py: Open theurls.pyfile in your Django project's main folder (myprojectin our example). Add a new path that maps to a view function responsible for rendering theindex.htmltemplate.# myproject/urls.py from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]Define a View Function: In the
views.pyfile within your Django application folder, importdjango.http.HttpResponseand define a view function namedindexthat returns an HTTP response containing the rendered HTML template.# myapp/views.py from django.http import HttpResponse from django.shortcuts import render def index(request): return render(request, 'index.html')Run the Development Server: Start the Django development server using the
manage.pyscript.python manage.py runserverAccess the URL specified in the
urls.pyfile (e.g.,http://127.0.0.1:8000/) in your web browser to view the rendered HTML template.Congratulations! You've successfully rendered an HTML template using Django's template system.
Rendering Templates with Data
In addition to rendering static HTML templates, Django allows passing dynamic data to templates, enabling the creation of dynamic and interactive web pages.
Let's enhance our index view function to pass some data to the template and render it accordingly.
Modify the View Function: Update the
indexview function inviews.pyto pass a dictionary containing dynamic data to the template.# myapp/views.py from django.shortcuts import render def index(request): context = { 'greeting': 'Hello there!', 'message': 'Welcome to our Django blog series.' } return render(request, 'index.html', context)In this modification, we've created a dictionary named
contextcontaining two key-value pairs:'greeting'with the value'Hello there!'and'message'with the value'Welcome to our Django blog series.'.Update the Template: Modify the
index.htmltemplate to display the dynamic data passed from the view function.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hello Django!</title> </head> <body> <h1>{{ greeting }}</h1> <p>{{ message }}</p> </body> </html>In this template, we use template variables
{{ greeting }}and{{ message }}to dynamically display the values passed from the view function.Run the Development Server: Start the Django development server using the
manage.pyscript.python manage.py runserverWhen you access the URL of your Django application (e.g.,
http://127.0.0.1:8000/) in your web browser, the webpage will display:Hello there! Welcome to our Django blog series.These values are dynamically populated using the data passed from the
indexview function to theindex.htmltemplate.
Conclusion
In this blog post, we covered creating an HTML template, defining a path in the urls.py file, and implementing a view function to render the template.
Stay tuned for the next blog, where we'll explore more advanced features of Django's template system, including template inheritance, context data, and template tags.
Happy templating!




