Django 1.0 Web Site Development Build powerful web applications, quickly and cleanly, with the Django application framework Ayman Hourieh BIRMINGHAM - MUMBAI Django 1.0 Web Site Development Copyright © 2009 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: March 2009 Production Reference: 1040309 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-847196-78-1 www.packtpub.com Cover Image by Raghuram Ashok ([email protected]) Credits Author Production Editorial Manager Ayman Hourieh Abhijeet Deobhakta Reviewer Editorial Team Leader Patrick Chan Abhijeet Deobhakta Senior Acquisition Editor Project Team Leader Douglas Paterson Lata Basantani Development Editor Project Coordinator Ved Prakash Jha Rajashree Hamine Technical Editor Proofreader Darshana D. Shinde Cathy Cumberlidge Copy Editor Production Coordinator Sneha M. Kulkarni Aparna Bhagat Indexer Cover Work Hemangini Bari Aparna Bhagat About the author Ayman Hourieh holds a bachelor's degree in Computer Science. He joined the engineering team at Google in January 2008. Prior to that, he worked with web application development for more than two years. In addition, he has been contributing to several open source projects such as Mozilla Firefox. Ayman also worked as a teaching assistant in Computer Science courses for one year. Even after working with a variety of technologies, Python remains Ayman's favorite programming language. He found Django to be a powerful and flexible Python framework that helps developers to produce high-quality web applications in a short time. I would like to thank my wife, Nadia, for all her help in writing this book. Not only did she provide support and motivation, but she also helped me greatly in proofreading and testing. I would also like to thank my mother and father for their continuous support and encouragement. About the reviewer Patrick Chan is an analyst programmer with Australia Post. He volunteers with Computer Bank, a not-for-profit organization that recycles donated computers and distributes them to disadvantaged individuals and community groups. He is currently involved in DjangoDB, a Django project that would upgrade Computer Bank's inventory system by allowing Computer Bank staff to manage its customer records, computer inventory, and transactions. DjangoDB would feature an improved interface with new features, such as enabling users to check for availabilities of computers and generate reports over the Web. Table of Contents Preface 1 Chapter 1: Introduction to Django 7 MVC pattern in web development 7 Why Python? 8 Why Django? 9 Tight integration between components 10 Object-Relational Mapper 10 Clean URL design 10 Automatic administration interface 10 Advanced development environment 10 Multilingual support 11 History of Django 11 Summary 12 Chapter 2: Getting Started 13 Installing the required software 13 Installing Python 13 Installing Python on Windows 14 Installing Python on UNIX/Linux 14 Installing Python on Mac OS X 15 Installing Django 15 Installing Django on Windows 15 Installing Django on UNIX/Linux and Mac OS X 16 Installing a database system 17 Creating your first project 18 Creating an empty project 18 Setting up the database 20 Launching the development server 22 Summary 23 Table of Contents Chapter 3: Building a Social Bookmarking Application 25 A word about Django terminology 25 URLs and views: creating the main page 26 Creating the main page view 26 Creating the main page URL 27 Models: designing an initial database schema 31 The link data model 32 The user data model 35 The bookmark data model 36 Templates: creating a template for the main page 38 Putting it all together: generating user pages 40 Creating the URL 40 Writing the view 41 Designing the template 42 Populating the model with data 44 Summary 45 Chapter 4: User Registration and Management 47 Session authentication 47 Creating the login page 48 Enabling logout functionality 53 Improving template structure 54 User registration 59 Django forms 59 Designing the user registration form 60 Account management 68 Summary 69 Chapter 5: Introducing Tags 71 The tag data model 72 Creating the bookmark submission form 75 Restricting access to logged-in users 80 Methods for browsing bookmarks 81 Improving the user page 83 Creating a tag page 85 Building a tag cloud 87 A word on security 90 SQL injection 91 Cross-Site Scripting (XSS) 91 Summary 93 [ ii ] Table of Contents Chapter 6: Enhancing the User Interface with AJAX 95 AJAX and its advantages 96 Using an AJAX framework in Django 97 Downloading and installing jQuery 98 The jQuery JavaScript framework 99 Element selectors 100 jQuery methods 100 Hiding and showing elements 101 Accessing CSS properties and HTML attributes 102 Manipulating HTML documents 103 Traversing the document tree 103 Handling events 104 Sending AJAX requests 105 What next? 105 Implementing live searching of bookmarks 105 Implementing basic searching 106 Implementing live searching 109 Editing bookmarks in place 112 Implementing basic bookmark editing 113 Implementing in-place editing of bookmarks 117 Auto-completion of tags 125 Summary 129 Chapter 7: Voting and Commenting 131 Sharing bookmarks on the main page 131 The SharedBookmark data model 132 Modifying the bookmark submission form 133 Browsing and voting for shared bookmarks 135 The popular bookmarks page 141 Commenting on bookmarks 144 Enabling the comments application 144 Creating a view for comments 145 Displaying comments and a comment form 147 Creating comment templates 149 Summary 153 Chapter 8: Creating an Administration Interface 155 Activating the administration interface 155 Customizing the administration interface 159 Customizing listing pages 160 Overriding administration templates 162 [ iii ]