Django pass variable from view to HTML template
In this article, you will learn how to pass data from a Django view to an HTML template.
As we know, Django keeps the front-end and back-end parts separate. The Django view takes a request and returns a response. The response can be an HTML page, an image, a pdf file, or xml. There may also be a need to pass some variables along with this response. The back-end files, like views or models, have been written in Python code, and the front-end template files have been written in basic HTML code. So the question is how to render the variable defined in 'views.py' into a template file.
In the template file, we can easily render Python variables within the curly braces {{}} in the template file.
In the given Python program, we have passed customer order details in a template file 'mytemp.html'. So first, we have taken some variables in views.py and rendered them to the template file-
views.py
from django.shortcuts import render
from django.template import Template, Context
import datetime
# Create your views here.
def order_details(request):
now = datetime.datetime.now()
person= {'firstname': 'Priska', 'lastname': 'Kashyap'}
item_list = {"Chocolate": 4, "Pen": 10, "Pencil": 3}
order_number= "000132342"
context= {
'person': person,
'item_list': item_list,
'order_number': order_number,
'current_date': now.date(),
}
return render(request, 'mytemp.html', context)
In the above file, we have imported the 'datetime' package and passed the current date using the 'now.date()' method in a variable. The item_list and person are dictionaries, which will be iterated in the template file.
mytemp.html
Here, we have rendered the variables within curly braces {{}}. To iterate over a dictionary, we have used a for loop in between {% %}. The forloop.counter counts the items starting with 1.
<html>
<head>
<title>Order Details</title>
</head>
<body>
<h1>Hello, {{ person.firstname }} {{ person.lastname }}</h1>
<p>Thanks for placing your order <b>{{order_number}}</b>
on <b>{{current_date}}</b>.</p>
<h2>Your Order Items :</h2>
{% for key, value in item_list.items %}
<p>{{ forloop.counter }}. {{ key }} : {{ value }}</p>
{% endfor %}
urls.py
Here, we have specified the path to render the page in the browser.
from django.contrib import admin
from django.urls import path
from demoapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('orderdetails', views.order_details),
]
After successful execution of the above code, call the url 'http://127.0.0.1:8000/orderdetails' on the browser. It will return the following result -
Related Articles
Django ajax GET and POST requestDjango Pagination with Ajax and jQuery
Django upload image to database
How to read and write a file using Django
Django pass variable from view to HTML template
Django Export Model Data to CSV
How to generate and download CSV file in Django
How to get data from MySQL in Django View with Models
Django Send Mail on Contact form submission
How to insert data in MySQL database from an HTML form using Django
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Django Send Mail on Contact form submission
Find the stop words in nltk Python
Python OpenCV Histogram of Grayscale Image
Eye Detection Program in Python OpenCV
Python program to check leap year