www.it-ebooks.info Beginning Django E-Commerce ■ ■ ■ JIM MCGAW www.it-ebooks.info Beginning Django E-Commerce Copyright © 2009 by Jim McGaw All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2535-5 ISBN-13 (electronic): 978-1-4302-2536-2 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Duncan Parkes Development Editor: Douglas Pundick Technical Reviewer: George Vilches Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Jim Markham Copy Editor: Ralph Moore Compositor: Mary Sudul Indexer: Carol Burbo Artist: April Milne Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales—eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. www.it-ebooks.info This book is dedicated to my parents. —Jim McGaw www.it-ebooks.info ■ CONTENTS Contents at a Glance ■About the Author....................................................................................................xi ■About the Technical Reviewer...............................................................................xii ■Acknowledgments................................................................................................xiii ■Introduction..........................................................................................................xiv ■Chapter 1: Best Laid Plans.......................................................................................1 ■Chapter 2: Creating a Django Site..........................................................................17 ■Chapter 3: Models for Sale....................................................................................39 ■Chapter 4: The Shopping Cart................................................................................79 ■Chapter 5: Site Checkout & Orders......................................................................109 ■Chapter 6: Creating User Accounts......................................................................153 ■Chapter 7: Product Images..................................................................................173 ■Chapter 8: Implementing Product Search...........................................................179 ■Chapter 9: Intelligent Cross-Selling.....................................................................193 ■Chapter 10: Adding in Ajax..................................................................................205 ■Chapter 11: Search Engine Optimization.............................................................231 ■Chapter 12: Web Security Overview....................................................................255 ■Chapter 13: Improving Performance...................................................................279 ■Chapter 14: Django Testing.................................................................................299 ■Chapter 15: Deployment......................................................................................323 ■Chapter 16: Django on Google App Engine..........................................................341 ■Index....................................................................................................................365 v www.it-ebooks.info ■ CONTENTS Contents ■About the Author..................................................................................................xvi ■About the Technical Reviewer.............................................................................xvii ■Acknowledgments..............................................................................................xviii ■Introduction..........................................................................................................xix ■Chapter 1: Best Laid Plans.......................................................................................1 Selling Stuff Online........................................................................................................................2 Why Django?..................................................................................................................................4 Straying From the Django Philosophy.......................................................................................5 A Quick Word on the Software..................................................................................................5 A Note on Version Control..........................................................................................................6 Firefox.......................................................................................................................................6 Installing the Software...................................................................................................................7 Installing Python........................................................................................................................7 Installing Django........................................................................................................................8 Installing MySQL........................................................................................................................9 Installing Eclipse and the PyDev Extensions.............................................................................9 Things to Consider Before You Start..............................................................................................9 Security...................................................................................................................................10 Accessibility............................................................................................................................10 PCI Compliance........................................................................................................................11 Search Engine Optimization....................................................................................................11 Deployment.............................................................................................................................12 Business Requirements...............................................................................................................12 Accounting & Auditing.............................................................................................................12 Supply Chain Management......................................................................................................13 Marketing Decisions................................................................................................................14 Summary......................................................................................................................................16 vii www.it-ebooks.info ■ CONTENTS ■Chapter 2: Creating a Django Site..........................................................................17 A Django-istic Welcome...............................................................................................................17 Creating the Project.................................................................................................................17 What Django Creates...............................................................................................................18 Creating the MySQL Database.....................................................................................................19 Dealing with Django Exceptions...................................................................................................22 Template & View Basics...............................................................................................................23 Advanced Templates with Inheritance.........................................................................................25 Greater Ease with render_to_response().....................................................................................28 Adding in the CSS........................................................................................................................29 Location, Location, Location........................................................................................................34 A Site Navigation Include.............................................................................................................35 A Word (or Two) About URLs........................................................................................................37 Summary......................................................................................................................................38 ■Chapter 3: Models for Sale....................................................................................39 Databases 101.............................................................................................................................40 An Introduction To SQL............................................................................................................40 What Makes a Relational Database.........................................................................................42 What Django Gives You – The ORM..............................................................................................44 Creating the Catalog App.............................................................................................................45 Creating the Django Models.........................................................................................................46 Model Field Data Types...........................................................................................................48 Creating the Category Model...................................................................................................50 Creating the Product Model.....................................................................................................54 The Django Admin Interface.........................................................................................................56 Product and Category Admins.................................................................................................57 A Note on Model Validation.....................................................................................................59 Syncing Up the Models............................................................................................................60 Playing with Model Structure and Data...................................................................................64 Templates, URLs, and Views........................................................................................................65 Configuring Page Titles and Meta Tags...................................................................................66 Coding Up the Catalog Views...................................................................................................68 Creating the Template Files.....................................................................................................69 A Category Link List.................................................................................................................73 viii www.it-ebooks.info ■ CONTENTS Our Code in Review......................................................................................................................73 So How Does It All Work?........................................................................................................75 File Not Found and Custom 404s.............................................................................................77 Summary......................................................................................................................................78 ■Chapter 4: The Shopping Cart................................................................................79 Shopping Cart Requirements.......................................................................................................79 An Introduction to Sessions.........................................................................................................80 The Shopping Cart Model........................................................................................................82 Django Sessions Overview......................................................................................................84 Enabling and Using Sessions..................................................................................................85 Using Django Forms.....................................................................................................................86 The Add To Cart Form..............................................................................................................86 Processing the Form...............................................................................................................88 Putting It All Together..............................................................................................................89 Cart Form Code in Review.......................................................................................................94 Creating the Shopping Cart Page.................................................................................................95 Django Template ‘if’ and ‘for’ Tags.........................................................................................96 Custom Template Filters.........................................................................................................96 Creating the Cart Page............................................................................................................97 Adding Custom Template Tags..................................................................................................101 Re-creating the Category List Tag.........................................................................................103 Static Content with Flatpages...............................................................................................105 Navigation Tags.....................................................................................................................107 Summary....................................................................................................................................108 ■Chapter 5: Site Checkout & Orders......................................................................109 Google Checkout API..................................................................................................................109 Signing up with Google Checkout..........................................................................................110 Submitting Orders to Google.................................................................................................111 Building XML Documents in Python.......................................................................................112 Making HTTP Requests in Python..........................................................................................114 Your Google Merchant ID and Key.........................................................................................115 The Python Property Decorator.............................................................................................115 Creating the Checkout App....................................................................................................116 ix www.it-ebooks.info ■ CONTENTS Order Checkout Requirements...................................................................................................124 SSL Middleware....................................................................................................................124 DRY Models and Forms.........................................................................................................126 Secure HTTP Requests..........................................................................................................128 Credit Card Transactions.......................................................................................................129 Order Checkout by Django.........................................................................................................130 Signing up for an Authorize.Net Test Account.......................................................................130 Order Information Models......................................................................................................131 The Checkout Form...............................................................................................................133 Authorization and Capture.....................................................................................................137 Order Processing...................................................................................................................139 Checkout Views and URLs.....................................................................................................141 Checkout Template and Order Form.....................................................................................143 Order Administration.............................................................................................................149 Summary....................................................................................................................................151 ■Chapter 6: Creating User Accounts......................................................................153 Making Friends with the Source................................................................................................153 Hooking Into Django’s Authentication........................................................................................154 Creating the Login & Registration Pages...............................................................................156 The My Account Page............................................................................................................160 The Change Password Page..................................................................................................162 The Order Details Page..........................................................................................................163 Django User Profiles...................................................................................................................165 Abstract Base Classes...........................................................................................................165 The Order Info Page...............................................................................................................167 Updating the Checkout Page.................................................................................................169 Summary....................................................................................................................................171 ■Chapter 7: Product Images..................................................................................173 Dealing with Images..................................................................................................................173 Django Image Fields..............................................................................................................174 Installing the Python Imaging Library....................................................................................174 Database Changes.................................................................................................................174 Editing the Model...................................................................................................................175 x www.it-ebooks.info ■ CONTENTS Adding a New Image.............................................................................................................176 Image Template Changes...........................................................................................................177 Summary....................................................................................................................................178 ■Chapter 8: Implementing Product Search...........................................................179 Instant Search............................................................................................................................179 Search Requirements.................................................................................................................180 Model Managers....................................................................................................................180 Complex Lookups with Q.......................................................................................................182 Search Results Pagination.....................................................................................................183 Implementing Search.................................................................................................................184 The Search Module................................................................................................................185 Search Template Tags...........................................................................................................187 Search View and Template....................................................................................................189 Third-Party Search Solutions.....................................................................................................192 Summary....................................................................................................................................192 ■Chapter 9: Intelligent Cross-Selling.....................................................................193 Product Page Recommendations...............................................................................................193 Order-Based Filtering............................................................................................................194 Customer-Based Order Filtering............................................................................................195 A Hybrid Approach.................................................................................................................195 Home Page Recommendations..................................................................................................196 Tracking Each User................................................................................................................197 Dealing with Searches...........................................................................................................198 View-Based Recommendations.............................................................................................200 Building the Homepage..............................................................................................................202 Summary....................................................................................................................................204 ■Chapter 10: Adding in Ajax..................................................................................205 The Ajax Pros and Cons.............................................................................................................205 How Ajax Works.........................................................................................................................207 jQuery for Ajax............................................................................................................................208 Getting jQuery........................................................................................................................208 jQuery Basics.........................................................................................................................209 xi www.it-ebooks.info