Book Image

Django Design Patterns and Best Practices

By : Arun Ravindran
Book Image

Django Design Patterns and Best Practices

By: Arun Ravindran

Overview of this book

Table of Contents (19 chapters)
Django Design Patterns and Best Practices
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Understanding Django's template language features


It is time to talk about the third musketeer in the MTV trio—templates. Your team might have designers who take care of designing templates. Or you might be designing them yourself. Either way, you need to be very familiar with them. They are, after all, directly facing your users.

Let's start with a quick primer of Django's template language features.

Variables

Each template gets a set of context variables. Similar to Python's string format() method's single curly brace {variable} syntax, Django uses the double curly brace {{ variable }} syntax. Let's see how they compare:

  • In Pure Python the syntax is <h1>{title}</h1>. For example:

    >>> "<h1>{title}</h1>".format(title="SuperBook")
    '<h1>SuperBook</h1>'
    
  • The syntax equivalent in a Django template is <h1>{{ title }}</h1>.

  • Rendering with the same context will produce the same output as follows:

    >>> from django.template import Template...