• No results found

Using the Generic Templates in the Rest of the App Templates

In document django-unleashed.pdf (Page 130-133)

Location for Templates

4.7.5 Using the Generic Templates in the Rest of the App Templates

simple, as shown in Example 4.54.

Example 4.54:Project Code

organizer/templates/organizer/base organizer.htmlin4791ef1567 1 {% extends parent_template|default:"base.html" %}

The blog base templateblog/templates/blog/base blog.htmlis identical, as

shown in Example 4.55. Example 4.55:Project Code

blog/templates/blog/base blog.htmlin27aebdc2e2 1 {% extends parent_template|default:"base.html" %}

This allows us to go back toorganizer/templates/organizer/tag list.html

and change the inheritance, as shown in Example 4.56. Example 4.56:Project Code

organizer/templates/organizer/tag list.htmlinb0c05bc701

1 {% extends parent_template|default:"organizer/base_organizer.html" %} Note that ourTaglist webpage has not changed. We’ve simply changed the inheritance structure to make our lives easier in the long run.

4.7.5

Using the Generic Templates in the Rest of the App

Templates

For the sake of brevity, we look at only one more example of how to integrate the generic template with our app template, in this case the list ofStartupobjects.

There are three steps to properly configuring each app template:

1. Surround the content in the file with{% block content %}and{% endblock %}.

2. Extend the app base template.

3. Override the title by using theblocktemplate tag.

The truth is that doing so for each and every app template is tedious and straightforward. In a normal project, we would typically start with the site-wide generic template before programming the app templates (where, in turn, we’d start with the app base templates). We

110 Chapter 4 Rapidly Producing Flexible HTML with Django Templates

did so in reverse to make the topic more approachable: it is easier to print a single variable than it is to inherit pre-existing templates.

Open/organizer/templates/organizer/startup list.html. Example 4.57 shows the current file.

Example 4.57:Project Code

organizer/templates/organizer/startup list.htmlin13ce1870d0 1 <h2>Startup List</h2>

2 <ul>

3 {% for startup in startup_list %} 4 <li>

5 <a href="">

6 {{ startup.name }}</a> 7 </li>

8 {% empty %}

9 <li><em>No Startups Available</em></li> 10 {% endfor %}

11 </ul>

We start by surrounding the content withblocktemplate tags. To properly invoke the override, the name of the block in our startup list template must be the same as the name of the block in the site-wide generic template, so we name the block in Example 4.58 content.

Example 4.58:Project Code

organizer/templates/organizer/startup list.htmlin9d05732bbc 7 {% block content %}

8 <h2>Startup List</h2> 9 <ul>

10 {% for startup in startup_list %} 11 <li>

12 <a href="">

13 {{ startup.name }}</a> 14 </li>

15 {% empty %}

16 <li><em>No Startups Available</em></li> 17 {% endfor %}

18 </ul>

19 {% endblock %}

This approach works only if we inherit the app base template (which inherits the project base template), so in Example 4.59, we use theextendstemplate tag to do so.

4.7 Using Template Inheritance for Design Consistency 111

Example 4.59:Project Code

organizer/templates/organizer/startup list.htmlin9d05732bbc

1 {% extends parent_template|default:"organizer/base_organizer.html" %} We may then override the title of our HTML page by again using theblocktemplate tag, this time referencing the title block, as shown in Example 4.60. You may optionally use the{{ block.super }}variable to refer to the value of the parents block.

Example 4.60:Project Code

organizer/templates/organizer/startup list.htmlin9d05732bbc 3 {% block title %}

4 {{ block.super }} - Startups 5 {% endblock %}

The final result is shown in Example 4.61. Example 4.61:Project Code

organizer/templates/organizer/startup list.htmlin9d05732bbc

1 {% extends parent_template|default:"organizer/base_organizer.html" %} 2

3 {% block title %}

4 {{ block.super }} - Startups 5 {% endblock %}

6

7 {% block content %} 8 <h2>Startup List</h2> 9 <ul>

10 {% for startup in startup_list %} 11 <li>

12 <a href="">

13 {{ startup.name }}</a> 14 </li>

15 {% empty %}

16 <li><em>No Startups Available</em></li> 17 {% endfor %}

18 </ul>

19 {% endblock %}

Consider that the content within the title block can be variable. For instance, in the

singleTagobject template,/organizer/templates/organizer/tag detail.html,

112 Chapter 4 Rapidly Producing Flexible HTML with Django Templates

Example 4.62:Project Code

organizer/templates/organizer/tag detail.htmlin0c87eb4aed 3 {% block title %}

4 {{ block.super }} - {{ tag.name|title }} 5 {% endblock %}

Remember that all of the code used in the book is available on GitHub, so you can see exactly what changes I’ve made for each file, should you wish to. However, following the three steps just outlined allows you to code correctly, even if we make different choices with regard to the title of the page.

4.8

Using Templates in Python with the

Template,

Context, and

loader

Classes

MVC’s View, Django’s template system, is the only part of Django to feature code that is not Python. It’s a safe bet that Django provides a way to import these template files into Python and to interact them with the other parts of Django. We now explore how to first load the template files we have written and then to output or render content.

In document django-unleashed.pdf (Page 130-133)