Speed up development with full-stack environments for every branch.

Learn More

Using template tags and filters in Django templates [Python]

Forked from Hello World in Django.

730 Runs 3738 Views 374 Copies
Saved

Saved

lgp171188 18

lgp171188
published 3 years ago

# Import the datetime module to get the current time
import datetime

# Import the render shortcut to render the templates in the response
from django.shortcuts import render

# This view renders the index.html template
def index(request):
    return render(request, "index.html")
    
# This view renders the for_loop.html template with a list    
def for_loop(request):
  employees = ["Guru", "Prasad", "Aditya", "Hari", "Kamal"]
  return render(request, "for_loop.html", {'employees' : employees})
  
# This view renders the for_loop.html template with an empty list
def for_loop_empty(request):
  employees = []
  return render(request, "for_loop.html", {'employees' : employees})

# This view renders the if_condition.html template with a number
def if_condition(request):
  age = 21
  return render(request, "if_condition.html", {'age' : age})
  
# This view renders the filters.html template with various variables.
def filters(request):
  context = {}
  context["number"] = 10
  context["text"] = "guruprasad"
  context["fruits"] = ['Apple', 'Orange', 'Banana', 'Mango', 'Papaya', 'Pomegranate']
  context["current_time"] = datetime.datetime.now()
  return render(request, "filters.html", context)
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).