Django Templates - Building User Interfaces


Django templates are essential for building user interfaces in web applications. They provide a way to separate HTML from Python code, making it easier to create dynamic and data-driven web pages. In this guide, we'll explore Django templates and how to use them to build user interfaces.


Before you begin, make sure you have the following prerequisites in place:

  • Django: You should have Django installed. If not, use
    pip install django
    to install it.
  • Django Project: You should have a Django project set up. If not, refer to the guide on creating your first Django project.
  • Django Views: Ensure you have views defined in your Django app. If not, refer to the guide on handling URL requests in Django.

Creating a Django Template

To create a Django template, follow these steps:

Sample Code

Create a new directory called "templates" within your Django app's directory. Inside this "templates" directory, create an HTML file. For example, create a file named "hello.html" with the following content:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Hello from Django</title>
<h1>Hello from Django!</h1>

Using Django Templates

To use a Django template in your view, you need to render it and return it as part of the HTTP response.

Sample Code

In your view function in
, render the template and return it as an HTTP response. For example:

from django.shortcuts import render
def hello(request):
return render(request, 'hello.html')


Django templates are a powerful tool for building user interfaces in your web applications. By separating HTML from Python code and rendering templates in your views, you can create dynamic and data-driven web pages that are easy to maintain and extend.