Book Image

Django JavaScript Integration: AJAX and jQuery

By : Jonathan Hayward
Book Image

Django JavaScript Integration: AJAX and jQuery

By: Jonathan Hayward

Overview of this book

<p>You want to create an AJAX application. Why would you use Django? Why would you use jQuery? Why would you use both together? Enter <i>Django JavaScript Integration: AJAX and jQuery</i> &ndash; 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.</p> <p>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 "<i>The web framework for perfectionists with deadlines</i>", how jQuery &ndash; the "<i>write less do more</i>" JavaScript library &ndash; is practically a virtual higher-level language, and why they both deserve to be integrated with AJAX.</p> <p>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.</p> <p>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.</p> <p>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".</p> <p>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.</p> <p>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.</p> <p>This book has been written and tested for Django v 1.2.3 and jQuery v 1.4.4.</p>
Table of Contents (20 chapters)
Django JavaScript Integration: AJAX and jQuery
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Chapter 5. Signing Up and Logging into a Website Using Ajax

Authentication and logins using Django Ajax can be accomplished using the following:

  • On the server-side, an @ajax_login_required decorator for views that is used similarly to Django's @login_required, and also server-side Ajax authentication handling.

  • On the client-side, a function that will, if an Ajax call returns a specific message saying authentication is needed, will provide an appropriate client-side div requesting login credentials and other details, and allow the request to be resubmitted after authentication has been cleared.

A decorator is a function that encapsulates another function and provides some added or modified functionality compared to the bare original. It is one of the patterns defined in the "gang of four" patterns book, "Patterns: Elements of Reusable Object-Oriented Software" by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides; Addison-Wesley Professional.

In this specific decorator, an inner function...