Django

Display a Variable Value Using a Template in Django

Introduction

In Django, we often need to send data from the view to the HTML page. This data is passed through a context dictionary, and the template displays it using {{ variable_name }}.

In this program, we will display a variable such as course = "MCA 202" on a web page.

Program Statement

Display a variable value, for example course = "MCA 202", using a template.


Step 1: Create Project and App

django-admin startproject myproject
cd myproject
python manage.py startapp myapp

Step 2: Register the App

1. File: settings.py

Path:

myproject/myproject/settings.py

Add myapp inside INSTALLED_APPS

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]

Step 3: Configure Templates Folder

2. File: settings.py

Path:

myproject/myproject/settings.py

Ensure the templates folder path is added in DIRS

import os

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

Step 4: Create the View

3. File: views.py

Path:

myproject/myapp/views.py

Code:

from django.shortcuts import render

def show_course(request):
data = {
'course': 'MCA 202'
}
return render(request, 'course.html', data)

Explanation

Here, a dictionary named data is created. It contains one variable called course. This dictionary is sent to the template using render().


Step 5: Create URL Mapping

4. File: urls.py

Path:

myproject/myproject/urls.py

Code:

from django.contrib import admin
from django.urls import path
from myapp import views

urlpatterns = [
path('admin/', admin.site.urls),
path('course/', views.show_course),
]

Step 6: Create HTML Template

5. File: course.html

Path:

myproject/templates/course.html

Code:

<!DOCTYPE html>
<html>
<head>
<title>Course Page</title>
</head>
<body>
<h1>Course Information</h1>
<h2>{{ course }}</h2>
</body>
</html>

Explanation

The variable is displayed using:

{{ course }}

This tells Django to replace it with the value passed from the view.


Step 7: Run the Server

python manage.py runserver

Open in browser:

http://127.0.0.1:8000/course/

Output

Course Information
MCA 202

How It Works

  1. The browser requests /course/
  2. Django checks urls.py
  3. It calls show_course()
  4. The view sends course = "MCA 202" to the template
  5. The template displays the value using {{ course }}

Common Errors

1. TemplateDoesNotExist

This error occurs if the course.html file is not inside the templates folder or the template path is not configured properly in settings.py.

2. Variable not displayed

If the variable name in the dictionary and in the template are different, Django will not show the value. For example, 'course' in view must match {{ course }} in template.

3. App not registered

If myapp is not added to INSTALLED_APPS, the project may not work correctly.


Viva Questions and Answers

1. What is context in Django?

Context is a dictionary used to pass data from a view to a template. It allows dynamic values to be displayed in HTML pages.

2. What is the use of render() in Django?

The render() function combines a template with context data and returns the final HTML response to the browser. It is commonly used to display pages.

3. How do you display a variable in a Django template?

A variable is displayed using double curly braces such as {{ course }}. Django replaces it with the actual value sent from the view.

4. What is the role of views.py in this program?

views.py contains the function that prepares data and sends it to the template. It acts as the connection between logic and presentation.

5. Why do we use a dictionary in the view?

A dictionary is used because multiple values can be passed easily in key-value form. The template can then access them by variable name.

6. What happens if the template variable name is wrong?

If the variable name is incorrect, Django will not display the value properly. In most cases, the output will appear blank where the variable should be shown.

7. What is a template in Django?

A template is an HTML file that can display static as well as dynamic data. It uses Django template syntax for variables and logic.

8. What is the purpose of urls.py here?

urls.py maps the URL /course/ to the show_course view. Without this mapping, the page cannot be accessed.

9. Can we pass more than one variable to a template?

Yes, we can pass multiple variables in the same dictionary. Each variable can then be displayed separately in the template.

10. Why is Django called a dynamic web framework?

Django is called dynamic because it can generate different output based on data from views, forms, or databases. It does not only serve fixed HTML pages.


Practice Questions

  1. Display subject = "Python Programming" in a template.
  2. Display teacher = "Dr. Kavita Srivastava" using a template.
  3. Pass two variables, course and semester, and show both on a page.
  4. Change the heading color using inline CSS in the template.

👉 Next Post: Display the Sum and Product of Two Fixed Numbers Using Template Rendering
👉 Back to List: Django Programs (60 Questions with Solutions)


Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *