Template Objects in Python

Introducting Template and Context


In [1]:
from django.template import Template, Context

In [2]:
template = Template('Hi, my name is {{ name }}.')

In [3]:
context = Context({'name': 'Andrew'})
template.render(context)


Out[3]:
'Hi, my name is Andrew.'

In [4]:
# no need to reload/recreate the template
context = Context({'name': 'Ada'})
template.render(context)


Out[4]:
'Hi, my name is Ada.'

In [5]:
template.render(Context())


Out[5]:
'Hi, my name is .'

In [6]:
template.render(Context({'name': 'Andrew'}))


Out[6]:
'Hi, my name is Andrew.'

In [7]:
template.render(Context({'Name': 'Andrew'}))


Out[7]:
'Hi, my name is .'

In [8]:
template = Template(
    '{{ ml.exclaim }}!\n'
    'she said {{ ml.adverb }}\n'
    'as she jumped into her convertible {{ ml.noun1 }}\n'
    'and drove off with her {{ ml.noun2 }}.\n'
)
mad_lib = {
    'exclaim':'Ouch',
    'adverb':'dutifully',
    'noun1':'boat',
    'noun2':'pineapple',
}
context = Context({'ml': mad_lib})
print(template.render(context))


Ouch!
she said dutifully
as she jumped into her convertible boat
and drove off with her pineapple.

Loading Templates from Disk


In [9]:
from django.template import loader

In [10]:
template = loader.get_template('organizer/tag_list.html')

In [11]:
best_list = [
    {'name': 'Pirates'},
    {'name': 'Ninjas'},
    {'name': 'Cowboys'},
]
context = Context({'tag_list': best_list})
print(template.render(context))


<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8">
    <title>
      

        Startup Organizer
       - Tag List

    </title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--[if IE]><script
      src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
    </script><![endif]-->
  </head>

  <body>

    <div><!-- container -->
      <header>
          <h1>Startup Organizer</h1>
      </header>
      <main>
        
  <h2>Tag List</h2>
  <ul>
    
      <li>
        <a href="">
          Pirates</a>
      </li>
    
      <li>
        <a href="">
          Ninjas</a>
      </li>
    
      <li>
        <a href="">
          Cowboys</a>
      </li>
    
  </ul>

      </main>
    </div><!-- container -->

    <footer>
      <p>
        &copy; 2015
        <a href="https://AndrewsForge.com/">
          Andrew Pinkham</a>
      </p>
      <p>
        Created for
        <a href="https://Django-Unleashed.com/">
          Django Unleashed</a>
      </p>
    </footer>

  </body>

</html>


In [12]:
from organizer.models import Tag

In [13]:
Tag.objects.all()


Out[13]:
[<Tag: Django>, <Tag: Mobile>, <Tag: Video Games>, <Tag: Web>]

In [14]:
# we don't actually need to reload the template
# code is here merely to emphasize the use of the loader
template = loader.get_template('organizer/tag_list.html')
context = Context({'tag_list': Tag.objects.all()})
print(template.render(context))


<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8">
    <title>
      

        Startup Organizer
       - Tag List

    </title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--[if IE]><script
      src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
    </script><![endif]-->
  </head>

  <body>

    <div><!-- container -->
      <header>
          <h1>Startup Organizer</h1>
      </header>
      <main>
        
  <h2>Tag List</h2>
  <ul>
    
      <li>
        <a href="">
          Django</a>
      </li>
    
      <li>
        <a href="">
          Mobile</a>
      </li>
    
      <li>
        <a href="">
          Video Games</a>
      </li>
    
      <li>
        <a href="">
          Web</a>
      </li>
    
  </ul>

      </main>
    </div><!-- container -->

    <footer>
      <p>
        &copy; 2015
        <a href="https://AndrewsForge.com/">
          Andrew Pinkham</a>
      </p>
      <p>
        Created for
        <a href="https://Django-Unleashed.com/">
          Django Unleashed</a>
      </p>
    </footer>

  </body>

</html>