Django HTML Example

Ann Jul 09, 2026

Django, a high-level Python Web framework, is renowned for its robustness and versatility. When it comes to creating HTML templates in Django, the process is straightforward and efficient. Let's delve into an example that demonstrates how to create an HTML template and use it in a Django view.

Django Template Language (DTL) Basics
Django Template Language (DTL) Basics

Before we dive into the code, it's crucial to understand that Django uses a template language to insert dynamic content into your HTML. This language is quite simple and allows you to output variables, perform loops, and use conditional statements.

🗑️ Template for Delete Page in Django
🗑️ Template for Delete Page in Django

Creating an HTML Template

Let's start by creating a simple HTML template named 'index.html' in a directory named 'templates' within our app. Django follows the convention that templates should be stored in a directory named 'templates' within each app.

Django Models Cheat Sheet
Django Models Cheat Sheet

Here's a basic example of what 'index.html' might look like:

```html My Django App

Welcome to my Django App!

Basic CRUD Structure in Django
Basic CRUD Structure in Django

This is a simple HTML template in Django.

```

Using the Template in a Django View

Now that we have our template, let's use it in a Django view. In your app's 'views.py' file, you'll need to import the template and render it with some data.

🔗 Django Models: OneToOneField (One-to-One Relationship)
🔗 Django Models: OneToOneField (One-to-One Relationship)

Here's how you can do it:

```python from django.shortcuts import render def index(request): data = { 'message': 'Hello, World!' } return render(request, 'index.html', data) ```

Passing Data to the Template

In the above code, we're passing a dictionary 'data' to the 'render' function. This dictionary contains key-value pairs that we can use in our template. Let's update our 'index.html' template to use this data:

Add Links with the {% url %} Tag in Django Templates
Add Links with the {% url %} Tag in Django Templates

```html My Django App

Welcome to my Django App!

{{ message }}

the diagram shows how to use django
the diagram shows how to use django
Customizing Views: Filter Lists in Django Admin
Customizing Views: Filter Lists in Django Admin
django-advanced-filters on Pypi
django-advanced-filters on Pypi
14 Django Website Template | Online Training Download app from below link
14 Django Website Template | Online Training Download app from below link
a diagram with the words django in different languages
a diagram with the words django in different languages
⚙️ Django Generic Views – Build Faster with Less Code
⚙️ Django Generic Views – Build Faster with Less Code
the web page for django
the web page for django
Secure Your Django Project with .env Files
Secure Your Django Project with .env Files
Understanding Context in Django Templates
Understanding Context in Django Templates
Django Project Structure Best Practice 2022
Django Project Structure Best Practice 2022
Create Views in Django
Create Views in Django
the bootstrap 4 dashboard is displayed in front of a cityscape background
the bootstrap 4 dashboard is displayed in front of a cityscape background
Django Views
Django Views
Django Ecommerce with Source Code
Django Ecommerce with Source Code
Why Django Framework is Best For Web Development?
Why Django Framework is Best For Web Development?
Django Insert Data Into Database with Source Code
Django Insert Data Into Database with Source Code
Django vs Flask: Which Python Framework Should You Learn First?
Django vs Flask: Which Python Framework Should You Learn First?
Django Models – choices Option
Django Models – choices Option
📄 Template with Model Relations in Django
📄 Template with Model Relations in Django
Learn Django Project  and File Structure
Learn Django Project and File Structure

```

In the template, we use double curly braces '{{ }}' to output the value of the 'message' variable. When the view is called, Django will replace '{{ message }}' with the value 'Hello, World!'.

Creating a List in a Django Template

Django templates also allow you to loop through lists. Let's add a list of items to our view and update our template to display it.

In your 'views.py', add a list to the 'data' dictionary:

```python def index(request): data = { 'message': 'Hello, World!', 'items': ['Item 1', 'Item 2', 'Item 3'] } return render(request, 'index.html', data) ```

Then, in your 'index.html', use the 'for' loop syntax to display the items:

```html My Django App

Welcome to my Django App!

{{ message }}

    {% for item in items %}
  • {{ item }}
  • {% endfor %}

```

With this, you've created a simple Django HTML template that displays dynamic content. Django's template language is powerful and flexible, allowing you to create complex templates with ease. Now, go forth and build your Django applications!