In Detail You want to create an AJAX application. Why would you use Django? Why would you use jQuery? Why would you use both together? Enter Django JavaScript Integration: AJAX and jQuery - your comprehensive answer to all these questions and the only extensive, practical, and hands-on guide to developing any AJAX application with Django and jQuery. Gone are the days when you used to lament over the lack of official documentation on AJAX with Django. This book will teach you exactly why Django is called " The web framework for perfectionists with deadlines ", how jQuery - the " write less do more " JavaScript library - is practically a virtual higher-level language, and why they both deserve to be integrated with AJAX. This hands-on-guide shows you how to put Django and jQuery together in the process of creating an AJAX application. In this book, they are brought together in a real-world scenario, with attention to usability, to build and develop an AJAX application. The first two chapters provide a short and necessary introduction to the world of Django, jQuery, and AJAX; and the remaining chapters are based on a case study that will make you realize the immense potential and benefits of integrating Django and jQuery with your AJAX application. By the time you are done with this book, you'll be developing your AJAX applications with Django and jQuery in less time than you can say "integrate". You will cover the basics of AJAX; use jQuery, the commonest JavaScript library, on the client side, and learn form validation with an eye to usability, build things with Django on the server-side, handle login and authentication via Django-based AJAX, and then dip into the rich jQuery plugin ecosystem to build in-place editing into your pages. You will add auto-complete functionality courtesy of jQuery UI, easily build forms with Django ModelForm, and then look at a client-side search implementation that can look things up without network access after initial download. You will learn to Implement a simple, expandable undo system, and offer more full-blooded account management, tinker, fix some bugs, offer a more usable way to handle password input, add local time support for people who are not in your time zone, look at usability, and finally take a look at debugging. After working through this book, you will have both an AJAX application and a deep understanding that you can use to customize, extend, and further develop it in your organization. This book has been written and tested for Django v 1.2.3 and jQuery v 1.4.4. Approach This book will teach you how to enrich your AJAX applications with Django and jQuery in a practical manner with minimum fuss. It introduces AJAX basics, gets a little deeper, and then goes chapter-by-chapter through a carefully chosen case study. It demonstrates how different pieces of a Django AJAX application work in themselves and are put together and specifically leads you through building an intranet employee photo directory with social networking features. Who this book is for This book is for people looking to integrate AJAX / JavaScript functionality into their web applications. It is for Django users who are looking to easily integrate AJAX features into their applications. Conversely, it will also be a priceless companion for users familiar with Django and jQuery who are and looking to integrate them in their AJAX applications. A working knowledge of Django and basic familiarity with AJAX and jQuery are assumed. Cover 1 Copyright 3 Credits 4 Foreword 5 About the Author 8 About the Reviewers 9 Table of Contents 12 Preface 18 Chapter 1: jQuery and Ajax Integration in Django 24 Ajax and the XMLHttpRequest object 25 Human speech: An overlaid function 25 Ajax: Another overlaid function 25 The technologies Ajax is overlaid on 26 JavaScript 26 XMLHttpRequest 31 Methods 31 Properties 32 HTML/XHTML 34 XML 35 JSON 35 CSS 36 The DOM 36 iframes and other Ajax variations 37 JavaScript/Ajax Libraries 38 Server-side technologies 38 A look at Django 38 Django templating kickstart 39 A more complete glimpse at Django templating 40 Setting JavaScript and other static content in place 49 Summary 50 Chapter 2: jQuery—the Most Common JavaScript Framework 52 jQuery and basic Ajax 53 jQuery Ajax facilities 56 $.ajax() 56 context 57 data 59 dataFilter 60 dataType 60 error(XMLHttpRequest, textStatus, errorThrown) 61 success(data, textStatus, XMLHttpRequest) 61 type 61 url 61 $.aj0axSetup() 62 Sample invocation 62 $.get() and $.post() 62 .load() 63 jQuery as a virtual higher-level language 65 The selectors 65 A closure-based example to measure clock skew 69 Case study: A more in-depth application 73 Chapter 3: Validating Form Input on the Server Side 73 Chapter 4: Server-side Database Search with Ajax 73 Chapter 5: Signing Up and Logging into a Website Using Ajax 74 Chapter 6: jQuery In-place Editing Using Ajax 74 Chapter 7: Using jQuery UI Autocomplete in Django Templates 74 Chapter 8: Django ModelForm: a CSS Makeover 74 Chapter 9: Database and Search Handling 74 Chapter 10: Tinkering Around: Bugfixes, Friendlier Password Input, and a Directory That Tells Local Time 75 Chapter 11: Usability for Hackers 75 Appendix: Debugging Hard JavaScript Bugs 75 Summary 75 Chapter 3: Validating Form Input on the Server Side 78 The standard lecture: low-level validation 79 Matching regular expressions 79 You cannot guarantee absolutely valid data 80 Validating can detect (some) malicious input 80 The Django way of validation 81 Django gives you some things for free 81 The steps in Django's validation 82 A more sensible and cruelty-free approach to validation 83 Things get murkier 84 The zero-one-infinity rule: a cardinal rule of thumb in usability 85 An improvement on Django's advertised approach 85 A validation example: GPS coordinates 87 Avoiding error messages that point fingers and say, "You're wrong!" 88 Validation as demanding that assumptions be met 89 Old-school: conform to our U.S.-based assumptions! 89 Adding the wrong kind of band-aid 91 Making assumptions and demanding that users conform 93 At least names are simple, right? 93 Even in ASCII, things keep getting murkier 94 Better validation may be less validation 95 Caveat: English is something of a lingua franca 96 We don't have to negotiate with pistols 97 Doing our best to solve the wrong problem: a story 98 It really does apply to validation 99 Facebook and LinkedIn know something better 100 Summary 100 Chapter 4: Server-side Database Search with Ajax 102 Searching on the client side and server side 103 Handling databases through Django models 103 Models for an intranet employee photo directory 104 Searching our database 112 A tour of Django persistence facilities 117 Summary 120 Chapter 5: Signing Up and Logging into a Website Using Ajax 122 admin.py: administrative functions called once 124 functions.py: project-specific functions, including our @ajax_login_required decorator 124 views.py: functions that render web pages 125 style.css: basic styling for usability 130 search.html: a template for client-side Ajax 131 The Django admin interface 139 Summary 141 Chapter 6: jQuery In-place Editing Using Ajax 142 Including a plugin 144 How to make pages more responsive 144 A template handling the client-side requirements 145 The bulk of the profile 149 Whitespace and delivery 150 Page-specific JavaScript 153 Support on the server side 154 Summary 156 Chapter 7: Using jQuery UI Autocomplete in Django Templates 158 Adding autocomplete: first attempt 159 Progressive enhancement, a best practice 159 A real-world workaround 163 "Interest-based negotiation": a power tool for problem solving when plan A doesn't work 163 A first workaround 165 Boilerplate code from jQuery UI documentation 171 Turning on Ajax behavior (or trying to) 173 Code on the server side 173 Refining our solution further 176 Summary 180 Chapter 8: Django ModelForm: a CSS Makeover 182 "Hello, world!" in ModelForm 182 Expanding and customizing the example 185 Customizing ModelForm pages' appearance 187 Going under ModelForm's hood 199 An excellent "stupid" question: where's the e-mail slot? 201 Summary 204 Chapter 9: Database and Search Handling 206 Moving forward to an AHAH solution 206 Django templates for simple AHAH 209 Templating for a list of search results 209 Template for an individual profile 212 Views on the server side 219 Telling if the user is logged in 219 A view to support deletion 219 The AHAH view to load profiles 220 Helper functions for the AHAH view for searching 221 An updated model 223 An AHAH server-side search function 224 Handling the client-side: A template for the main page 226 CSS for styling the directory 249 Our updated urlpatterns 258 Summary 258 Chapter 10: Tinkering Around: Bugfixes, Friendlier Password Input, and a Directory That Tells Local Time 260 Minor tweaks and bugfixes 260 Setting a default name of "(Insert name here)" 261 Eliminating Borg behavior 261 Confusing jQuery's load() with html() 262 Preventing display of deleted instances 263 Adding a favicon.ico 266 Handling password input in a slightly different way 267 A directory that includes local timekeeping 269 Summary 277 Chapter 11: Usability for Hackers 278 Usability begins with anthropology... and Django hackers have a good start on anthropology 279 Anthropological usability techniques 280 An introductory example: card sorting 280 Focus groups: cargo cult research for usability 282 Anthropological observation: the bedrock of usability 282 More than one way to see the same situation 283 Applying this foundation to usability 285 It's just like (hard) debugging 288 Lessons from other areas 289 Live cross-cultural encounters 289 History 290 Old books and literature 291 The last other area: whatever you have 294 Understanding the user 295 A lesson from optimization 295 What's wrong with scratching an itch, or you are not your user 296 Worst practices from the jargon file 296 Python and usability 297 It's not all about the computer! 297 What to do in the concrete 299 Further reading 300 Summary 301 Appendix: Debugging Hard JavaScript Bugs 302 "Just fiddling with Firebug" is considered harmful 302 Cargo cult debugging at your fingertips 302 The scientific method of debugging 303 Exhausting yourself by barking up the wrong tree 304 The humble debugger 306 The value of taking a break 306 Two major benefits to asking for help 307 Firebug and Chrome developer tools 307 The basics across browsers 307 Zeroing in on Chrome 310 Summary 315 Index 316 In Detail You want to create an AJAX application. Why would you use Django? Why would you use jQuery? Why would you use both together? Enter Django JavaScript Integration: AJAX and jQuery - your comprehensive answer to all these questions and the only extensive, practical, and hands-on guide to developing any AJAX application with Django and jQuery. Gone are the days when you used to lament over the lack of official documentation on AJAX with Django. This book will teach you exactly why Django is called "The web framework for perfectionists with deadlines", how jQuery - the "write less do more" JavaScript library - is practically a virtual higher-level language, and why they both deserve to be integrated with AJAX. This hands-on-guide shows you how to put Django and jQuery together in the process of creating an AJAX application. In this book, they are brought together in a real-world scenario, with attention to usability, to build and develop an AJAX application. The first two chapters provide a short and necessary introduction to the world of Django, jQuery, and AJAX; and the remaining chapters are based on a case study that will make you realize the immense potential and benefits of integrating Django and jQuery with your AJAX application. By the time you are done with this book, you'll be developing your AJAX applications with Django and jQuery in less time than you can say "integrate". You will cover the basics of AJAX; use jQuery, the commonest JavaScript library, on the client side, and learn form validation with an eye to usability, build things with Django on the server-side, handle login and authentication via Django-based AJAX, and then dip into the rich jQuery plugin ecosystem to build in-place editing into your pages. You will add auto-complete functionality courtesy of jQuery UI, easily build forms with Django ModelForm, and then look at a client-side search implementation that can look things up without network access after initial download. You will learn to Implement a simple, expandable undo system, and offer more full-blooded account management, tinker, fix some bugs, offer a more usable way to handle password input, add local time support for people who are not in your time zone, look at usability, and finally take a look at debugging. After working through this book, you will have both an AJAX application and a deep understanding that you can use to customize, extend, and further develop it in your organization. This book has been written and tested for Django v 1.2.3 and jQuery v 1.4.4.Approach This book will teach you how to enrich your AJAX applications with Django and jQuery in a practical manner with minimum fuss. It introduces AJAX basics, gets a little deeper, and then goes chapter-by-chapter through a carefully chosen case study. It demonstrates how different pieces of a Django AJAX application work in themselves and are put together and specifically leads you through building an intranet employee photo directory with social networking features.Who this book is for This book is for people looking to integrate AJAX / JavaScript functionality into their web applications. It is for Django users who are looking to easily integrate AJAX features into their applications. Conversely, it will also be a priceless companion for users familiar with Django and jQuery who are and looking to integrate them in their AJAX applications. A working knowledge of Django and basic familiarity with AJAX and jQuery are assumed Annotation. You want to create an AJAX application. Why would you use Django? Why would you use jQuery? Why would you use both together? Enter Django JavaScript Integration: AJAX and jQuery _ your comprehensive answer to all these questions and the only extensive, practical, and hands-on guide to developing any AJAX application with Django and jQuery. Gone are the days when you used to lament over the lack of official documentation on AJAX with Django. This book will teach you exactly why Django is called "The web framework for perfectionists with deadlines", how jQuery _ the "write less do more" JavaScript library _ is practically a virtual higher-level language, and why they both deserve to be integrated with AJAX. This hands-on-guide shows you how to put Django and jQuery together in the process of creating an AJAX application. In this book, they are brought together in a real-world scenario, with attention to usability, to build and develop an AJAX application. The first two chapters provide a short and necessary introduction to the world of Django, jQuery, and AJAX; and the remaining chapters are based on a case study that will make you realize the immense potential and benefits of integrating Django and jQuery with your AJAX application. By the time you are done with this book, you'll be developing your AJAX applications with Django and jQuery in less time than you can say "integrate". You will cover the basics of AJAX; use jQuery, the commonest JavaScript library, on the client side, and learn form validation with an eye to usability, build things with Django on the server-side, handle login and authentication via Django-based AJAX, and then dip into the rich jQuery plugin ecosystem to build in-place editing into your pages. You will add auto-complete functionality courtesy of jQuery UI, easily build forms with Django ModelForm, and then look at a client-side search implementation that can look things up without network access after initial download. You will learn to Implement a simple, expandable undo system, and offer more full-blooded account management, tinker, fix some bugs, offer a more usable way to handle password input, add local time support for people who are not in your time zone, look at usability, and finally take a look at debugging. After working through this book, you will have both an AJAX application and a deep understanding that you can use to customize, extend, and further develop it in your organization. This book has been written and tested for Django v 1.2.3 and jQuery v 1.4.4 'This is a practical hands-on book with clear instructions and lot of code examples. It takes a simple approach, guiding you through different architectural topics using realistic sample projects. A single project is implemented using different architectural styles to make the reader understand the details of each style. There are also many small independent code samples to explain design patterns, WCF, and localization. This book is for people familiar with the ASP.NET framework using either C# or VB.NET. You don't need to be an ASP.NET guru – the book is ideal for novice and intermediate developers. If reading about application architecture usually confuses you or sends you to sleep, then this book will be perfect for you! In short, any ASP.NET programmer who is confused or disoriented reading different books or materials on architectures wondering how and what to implement in their application, will definitely benefit from this book!'