Book Image

Django 1.0 Template Development

Book Image

Django 1.0 Template Development

Overview of this book

Table of Contents (17 chapters)
Django 1.0 Template Development
Credits
About the Author
About the Reviewers
Preface
Index

Preface

Django is a high-level Python web application framework designed to support the rapid development of dynamic web sites, web applications, and web services. It includes a template system that allows programmers and designers to easily and efficiently output their content in a flexible, extendable, and maintainable manner.

This book is a comprehensive, practical exploration of Django's template system. Developers and template authors will appreciate the introduction to Django templates, including an examination of views, generic views, and URL configurations to illustrate how incoming requests are handled and ultimately mapped to templates. Template inheritance and outputting different templates based on user agents are also covered.

The chapters on pagination, internationalization, caching, and customizing the admin application are example-driven so you can learn the concepts and later apply them as "recipes" in your own projects. For most examples, we will be working with an ongoing example project to show the power of combining your new skills together.

Whether large or small, complex or simple, I hope the techniques presented in this book serve you well in your Django projects.

What this book covers

Here is a brief summary of each chapter:

Chapter 1 gives you an introduction to the Django template system and provides an overview of how it works.

Chapter 2 explores how URL configuration routes your requests to views and generic views. You will understand how to use generic views to streamline your project's development.

Chapter 3 explains how data from your views is exposed to the template system via the template context.

Chapter 4 reviews all of Django's built-in tags and filters, each with examples and usage notes.

Chapter 5 uses extension and inheritance to create a modular skeleton for your project's templates.

Chapter 6 teaches you how to serve multiple versions of your templates in a single Django project. You will create mobile and traditional templates and learn how to serve them from the same views.

Chapter 7 explains how to extend the template system by writing your own template tags and filters.

Chapter 8 teaches you how to use Django's pagination libraries to split the output of your applications into pages and provide navigation between them.

Chapter 9 shows you how to customize the look and feel of the automatic admin application by editing some templates and creating custom CSS rules.

Chapter 10 teaches you to use the cache framework to optimize the speed and performance of your project.

Chapter 11 uses internationalization to automatically serve your site templates in multiple languages based on the user's preferences.

What you need for this book

  • A working installation of Python 2.3 or greater (2.4 or greater is recommended)

  • The ability to run a Django-supported database (examples in the book use SQLite)

  • An installed and working Django installation (see www.DjangoProject.com for installation details)

  • Some experience with Django, at least having gone through the tutorials at www.DjangoProject.com

Who this book is for

This book is for web developers and template authors who want to fully understand and utilize the Django template system. The reader should have completed the introductory tutorials on the Django project's web site and some experience with the framework will be very helpful. Basic knowledge of Python and HTML is assumed.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can include other contexts through the use of the include directive."

A block of code will be set as follows:

{% ifequal color 'blue' %}
  Wow, you like blue!
{% else %}
  Why don't you like blue?
{% endifequal %}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be made bold:

from django.http import HttpResponse
from django.template import Context, Template, loader
def detail(request):
    dict_values = {'fav_color': 'blue'}
    template_string = "My favorite color is {{ fav_color }}."
    c = Context(dict_values)
    t = Template(template_string)
    rendered_template = t.render(c)
    return HttpResponse(rendered_template)

Any command-line input and output is written as follows:

$ python manage.py runserver

New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to , making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code for the book

Visit http://www.packtpub.com/files/code/5708_Code.zip to directly download the example code.

Note

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide the location address or web site name immediately so we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with some aspect of the book, and we will do our best to address it.