Public pages: Templates

  1. Using the Django API
  2. Public pages
  3. Template inheritance

http://docs.djangoproject.com/en/dev/ref/templates/builtins/

The Django template language is a very simple templating language. It uses {{ … }} to refer to the things you put in the “context” that you send the template. It uses {% … %} to refer to structural code, such as “for… endfor”. If something has a property or a method, you can use the period to access that property or method. For example, “postings” was the list of posts we sent it. Each “post” has a title on it, so {{ post.title }} is that title.

Let’s fill out the blog’s main page. Replace “<h2>{{ post.title }}</h2>” with:

<h2>{{ post.title }}</h2>

<p class="author">{{ post.author }}, {{ post.date|date:"F jS, Y h:i a" }}</p>

{{ post.content|linebreaks }}

<p class="topics">{{ post.topics.all|join:", " }}</p>

Besides post.xxxx, this template also includes filters. The post’s date is filtered through the “date” filter. See the Django documentation for what those letters mean and what other letters are available. The post’s content is filtered through “linebreaks”, which takes all double blank lines and turns them into paragraphs, and all single blank lines and turns them into <br /> tags.

Finally, the list of all post topics is joined together on a comma using the “join” filter. The “:” after a filter gives the filter parameters.

  1. Using the Django API
  2. Public pages
  3. Template inheritance