Back to description
You had me at “Hello.”
Ren~Ee Zellweger in Jerry Maguire
The authors know you. Or at least they know a few things about you... more
The authors know you. Or at least they know a few things about you. If you’ve grabbed this book from your favorite online (or even brick-and-mortar) bookseller, chances are that you’re an accomplished Python developer, or you’re on your way to becoming one. You may well have created web content using your Python skills, or perhaps you’ve written your share of web services backed by Python code as a Common Gateway Interface (CGI) script. You may have just heard the term “Web 2.0” or “AJAX,” or you may already be an accomplished user of one or many Web 2.0 sites and are wondering how to plug into the action as a developer. You may be aware of web frameworks in other languages, such as Ruby on Rails (also referred to simply as Rails) or Google Widget Toolkit (GWT), and you may even have experimented with one or more them. If you’ve been unable to embrace any of these other frameworks, it may be because they have one annoying aspectthey’re not Python. Actually, if you’ve worked with other frameworks, the fact that they don’t follow the Python philosophy (they’re not Pythonic) may be the least of your negative impressions.
The good news is that now you don’t have to abandon the language that you love to get the (admittedly significant) benefits of a comprehensive framework. The Python-based frameworks covered in the chapters that follow are every bit as capable and fun to use as any other, and it might be argued, perhaps even better in some ways. The Rails language, for example, demands a certain adherence to convention that sometimes gets in the way of a coder’s freedom of expression. GWT is essentially a Java-based page-element composer that enables you to bypass writing ECMAScript directly, but lacks the design center for dynamic partial page replacement for which the Python Turbogears framework is becoming renowned.
... less
Each success only buys an admission ticket to a more difficult problem.
Henry Kissinger
All of the frameworks and tools shown... more
All of the frameworks and tools shown in this book rely on some fairly simple underlying technologies, like JavaScript, Pyth which have been used since the dawn of Web 1.0. In this chapter, you’ll see how you can build Web 2.0 applications using just those old-school technologies. Even if you decide to use higher-level frameworks and tools (and you will), you’ll still benefit from seeing how the nuts and bolts work together at the ground level. The example at the end of this chapter illustrates how you can build a simple Web 2.0 application using just these traditional tools. The rest of this book shows how you can use frameworks to make the job easier, more efficient, and more fun.
Out of intense complexities intense simplicities emerge.
Winston Churchill
If we wish to count lines of code, we should not... more
If we wish to count lines of code, we should not regard them as lines produced but as lines spent.
Edsger Dijkstra
At the dawn of the Internet age, web developers designed simple web pages that returned simple HTML for a simple request. Since then, the web page has grown increasingly complex. The role of a web site has evolved from the simple act of presenting a user with a view of information, to a Rich Internet Application that can provide the user with a highly interactive experience. Applications like word processors, spreadsheets, and calendars can exist exclusively online and have the capabilities commonly found in a rich client application.
Of course all of these additional capabilities don’t come without a price. The technology necessary to produce complex behavior in web sites has also gotten increasingly complex. It all started with the incorporation of basic server-side PHP, and simple client-side JavaScript to make a web site more reactive to a user’s actions. Now a web site may have large, sometimes unwieldy sets of objects defined in Java or Python (among a slew of other possible languages) that retrieve the data from a database for presentation. There may be complex business objects that add domain logic and process the data before it’s presented. A site may also make use of increasingly complex interfaces that incorporate client-side JavaScript to create complex UI controls, manipulate the elements of a page on the fly, or make asynchronous calls to the server.
When faced with all of this complexity, it’s easy to get discouraged unless you bear in mind the advantages you can leverage when faced with these issues. Consider that most applications perform the same types of operations, like generating asynchronous requests, or harder work, like maintaining data in a database. These operations also typically include a lot of boilerplate code that can be greatly condensed.
Think about the asynchronous request that you created in Chapter 2, “Web 2.0 with Traditional Python.” It took 10 lines of code to instantiate the request and to ensure it would work in Internet Explorer and other browsers. Because you almost always want to create a request that works in any browser, you’re relegated to spend 10 lines of code on this operation in every application.
This leads to the development and use of frameworks that can save developers from having to rewrite the complex components to manage database interactions, or map URLs to operations that the application should perform. It also saves developers from having to write and read lines of duplicate or boilerplate code. Without frameworks, a developer is relegated to needlessly reinvent (or rewrite) the wheel with every new web application.
In this chapter, you’re given an introduction to the design of many web application frameworks, and you’ll learn the basic capabilities that they can provide. Then you’ll learn about the TurboGears and Django frameworks and get a chance to see them in action as you use them to re-implement the example from the previous chapter.
Simplicity is prerequisite for reliability.
Edsger W. Dijkstra
In Chapter 3 you were introduced to the TurboGears and the... more
In Chapter 3 you were introduced to the TurboGears and the Django frameworks. Here you’ll learn more about the capabilities of the TurboGears framework, common framework components, and the structure of a TurboGears project.
In this chapter you’ll gain experience with TurboGears as you implement a blog. You’ll start by setting up the project and defining a basic model. Then you’ll see how to use TGCrud, a controller and view generator, to create a variety of views for manipulating your model objects. Finally, you’ll learn about the capabilities of Kid templates as you modify the master design of the site.
How often, or on what system, the Thought Police plugged in any individual wire was guesswork. It was even conceivable that... more
How often, or on what system, the Thought Police plugged in any individual wire was guesswork. It was even conceivable that they watched everybody all the time. But at any rate, they could plug in your wire whenever they wanted to.
George Orwell in 1984
In the last chapter, you got a whirlwind introduction to TurboGears and created a usable blog application. In this chapter, you’ll build on that foundation as you learn the ins and outs of user identity and visitor tracking. The TurboGears Visit Tracking framework creates and tracks sessions associated with the users of your web application; the TurboGears Identity framework enables you to easily identify users and add security to a web application. More specifically, in this chapter you’ll learn the following:
How cookies are used to track known and anonymous user visits
The main components of the Identity framework:
User identity model objects that get generated when you create a new project
The main user identity configuration options available for a project
The main components provided by the user identity framework
The main components of the Visit Tracking framework:
The model objects used to track visitors
The main configuration options that modify the behavior of the visitor tracking framework
How to write your own visitor tracking plug-ins to record and analyze site visitor behavior
You’ll gain hands-on experience as you expand the TBlog application to make use of the users, groups, and security provided by the Identity framework. You’ll also use the Visit Tracking framework to collect and analyze information about your site’s visitors.
Note
This chapter builds on the example constructed in Chapter 4. Ideally, you have already read that chapter and explored the example, but it’s not absolutely required. If you’re comfortable with the basics of TurboGears and are interested in jumping right into user identity and visitor tracking, you can start with this chapter. Just be sure to download and familiarize yourself with the code from the previous chapter.
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.... more
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
Martin Golding
Regardless of the development language or type of application, developers should always attempt to develop well-organized and reusable code. If the organization of code is ignored for an application, the result is a tangle of duplicated code that is difficult to understand and maintain. In such systems, a capability is typically implemented, and then copied and pasted into other parts of the system as the need to reuse the capability is seen. This in turn makes it extremely difficult for you or others to maintain the code after it’s written. A single change may have a ripple effect and require a series of changes to many other similar pieces of code.
Any language provides a set of programming constructs that help you write organized, maintainable code with minimal duplication. Procedural languages use procedures and functions to help alleviate this issue by grouping common operations into logical chunks. These may be collected further and organized into packages, modules, and libraries. Object-oriented languages take this a step further with object specifications that enable you to group common functions and data into a single entity. Inheritance is useful to ensure that common capabilities are specified only once in a parent class, and inherited by subclasses.
In a web application, however, it’s not always possible to represent a single reusable entity with only the data and functions available in object-oriented languages. This is because a web application typically relies on a combination of three very distinct elements that may produce a reusable component only when taken together. The structure of the component is established with HTML, JavaScript is required to define client-side behaviors for the component, and finally style sheets are necessary to tailor the site’s appearance.
TurboGears provides widgets to enable a web application developer to write reusable components that include all the elements wrapped together in a single package. The ultimate effect is a reduction in duplicate code, a more maintainable and reusable component, and a more logical organization of all the code that is necessary to create a component.
In Chapter 4, “Introduction to TurboGears,” you were introduced to the concept of widgets when you used TGCrud to generate a simple interface to view, modify, and delete blog authors and entries. In this chapter you build on that introduction and explore the possibilities of the widget framework in more detail, including the following:
Widget design motivations and advantages
The Widget Browser, a tool used to catalog and document widgets
How to use existing widgets to easily add complex capabilities to a web application
How to implement and package your own widgets for reuse in one or more web applications
Throughout the chapter, you enhance the TBlog web application to represent, accept, and display user comments for a given blog entry. First you use existing form elements to accept new comments, and then you define your own widget that can be used to display user comments.
This chapter builds on the TBlog example from Chapters 4 and 5. You may want to explore those chapters before starting here, but it isn’t absolutely required. Before starting off with the widget examples here, however, you should download and review the Chapter 5 version of the TBlog application from the book’s site.
It’s not easy being green.
Kermit the Frog
In addition to being green, there are several other things that aren’t easy. This... more
In addition to being green, there are several other things that aren’t easy. This is especially true of learning to create applications with a multifaceted framework. After the first examples you encounter, the framework seems almost magicalas though achieving a working application happens almost at the speed of thought. Then you begin to hit stone walls when the next application’s nuances and subtleties starts testing the limits of the framework, or at least what you think you know about the framework. In the worst-case scenario, you begin to see the imperfections and shortcomingsplaces where the framework’s design center seems to have failed to take into account something you urgently need in your application, or alternatively, perhaps the flaw is simply in your own level of understanding of how the framework was intended to function.
This chapter is dedicated to presenting all those other things you need to know when working with TurboGearsthose things that you encounter quickly after departing the familiar terrain of pages and single table models. Based on delving into the TurboGears forums and on the authors’ own experiences in doing moderate (to hard) TurboGears projects, here are some of the questions you are bound to encounter:
Are there helper tools that I can use to aid my thinking on the application design, or that generate code (beyond what TurboGears does for me already)?
How do I integrate TurboGears with other web services? Does TurboGears preclude doing mashups with other application programming interfaces (APIs)?
How do I get information from multiple parts of the model? How do I do one-to-one and one-to-many table joins?
How do I coordinate elements of the visual model the RIA way (for example, coordinating map and table data)?
How do I do nifty on-page effects that seem to be everywhere in Web 2.0 applications?
How do I create those cool RSS feeds that everyone finds so useful?
Look with favor upon a bold beginning
Virgil in The Georgics
Django is a high-level Python web framework that supports rapid... more
Django is a high-level Python web framework that supports rapid development and elegant design. In this chapter you’ll see how Django works to automate production of common web-development tasks so that the developer can concentrate on higher-level application issues instead of low-level data handling. You’ll see how Django supports the model-view-controller (MVC) separation of concerns in a way that makes development easier and more productive.
I don’t want someone shoving his views down my throat, unless they’re covered in a crunchy candy shell.... more
I don’t want someone shoving his views down my throat, unless they’re covered in a crunchy candy shell.
Stephen Colbert
In the previous chapter you saw some of the great features that make getting started with Django easy. Django’s support for model-view-controller (MVC) web applications, automatic object-relational mapping (ORM), and powerful template processor make setting up simple web applications a snap. In this chapter you’ll learn about additional Django features that address some of the issues affecting large-scale web applications. You’ll see examples of how to reduce duplicated code through template reuse, how to use Django’s generic views to avoid writing boilerplate view methods, and how to use Django’s built-in capability for user authentication and authorization to manage user accounts and permissions.
Although the simple blog application presented in the previous chapter worked, there are some parts of its design that are less than ideal. For example, all of the templates contain text that is exactly the same for all pages. This makes maintenance more difficult than necessary because any change to that common text will have to be propagated correctly to all of the other copies. This is a very error-prone and labor-intensive process. Django’s template inheritance enables you to put that common text in one template file and reference it from other template files.
Similarly, many of the view methods were very simple, consisting of the following two steps:
1. Populate a context from the database.
2. Render a template using that context.
Django includes a set of generic views that can perform these two steps and don’t require you to write and maintain any view methods.
Finally, the blog application you created in the previous chapter would allow anyone who wanders in off the Internet to make blog entries and comments. In this chapter you see how to use Django’s built-in authentication and authorization framework to control access permissions.
The future is here. It’s just not widely distributed yet.
William Gibson
Now that you’ve seen some of the basics of how to... more
Now that you’ve seen some of the basics of how to use Django and some of the ways it can make your life easier, it’s time to turn to the more advanced features of Django. Not that these things are hard to do (they’re not), but they’re the kind of features that separate the amateur home page from the professional web application.
Asynchronous JavaScript and XML (AJAX) is all the rage lately as a technique for building dynamic web pages that can be updated without a full page refresh, which traditional web usage requires. The essence of AJAX is JavaScript on the web page that makes requests for data from the server and updates Document Object Model (DOM) elements on the page while the user is viewing and interacting with the page. In this chapter, you learn how to use Django to handle those back-end requests.
When you’re deciding which, if any, framework to adopt for your project, it’s important to consider the availability of add-on modules that may not be part of the core code base, but can be leveraged when you develop your site. This chapter provides an overview of some add-on modules available for Django and discusses the Really Simple Syndication (RSS) module in detail.
You’ll also see some options for deploying Django with the popular Apache web server. Apache is a great choice for deploying Django applications because it’s free, it’s good, and the mod_python Apache module makes integration simple.
mod_python
Once in seven years I burn all my sermons; for it is a shame if I cannot write better sermons now than I did seven years... more
Once in seven years I burn all my sermons; for it is a shame if I cannot write better sermons now than I did seven years ago.
John Wesley
Rather like the prescription to start fresh periodically offered by the founder of Methodism, JavaScript (in the opinion of many frustrated web developers) should have taken a trip back to the drawing board at least a couple of times for a complete rewrite. Since its inception and deployment in Netscape 2 in 1995, JavaScript (now officially referred to as ECMAScript), has undergone a number of ad hoc repairs, but it’s still the same basic language: syntactically similar to languages like Java and C, but with semantics that are utterly different and an object model unrelated to and largely incompatible with Java or Python.
Small wonder that as the need for a robust in-browser language has grown from nice-to-have to absolutely critical, a number of generally similar JavaScript library collections have appeared in the open source community. Perhaps unsurprisingly, Django and TurboGears are the epitome of tough employers when it comes to libraries and frameworks. Although they are both quite capable of working with a number of underlying frameworks, they will toss the current choice aside when something better comes along. TurboGears, for example is being re-based to move to Genji and SQLAlchemy. In the case of the preferred JavaScript library, though, it’s unlikely that either Django developers or TurboGears developers will want to move away from MochiKitit’s quite simply the best of its ilk. Not only that; its design center makes it something that most definitely appeals to Pythonists.
You don’t understand! I coulda had class. I coulda been a contender. I could’ve been somebody.
Marlon Brando in On the Waterfront... more
Marlon Brando in On the Waterfront
Thus far in this book, you’ve seen the user interface or view portion of Rich Internet Applications (RIAs) either hand-crafted in HTML and Cascading Style Sheets (CSS), or constructed dynamically on the server using a templating system such as Kid. This chapter covers the union of Flex with Python frameworksand make no mistake, this combination absolutely does have class, it is a contender, and it’s destined to be a somebody.
This appendix includes all the information you need to know to get up and running with the tools used in this book. All of... more
This appendix includes all the information you need to know to get up and running with the tools used in this book. All of the tools that have been used in this book are free or open source software, so it’s not a matter of expense to buy the software, but sometimes it can be quite a chore to scour the Internet looking for tools, and it can be equally difficult to get them to all work together after you’ve found them. Hopefully, this appendix can help ease some of that pain.
Purchase Before purchasing this product, please be sure you have met all software and system requirements, and that you understand any limits placed upon its use.
Return Policy Wrox Chapters on Demand are non-returnable and non-refundable.
Reader Software Wrox Chapters on Demand are offered as PDFs, and they must be viewed using the Adobe Reader. If you do not have the Reader installed, it can be downloaded for free at Adobe.com.
Test Download As Wrox Chapters on Demand purchases are non-returnable, it is advisable that you test your system and software configurations with a free sample download before you place an order.
Usage Rights for a Wrox Chapter on Demand File Any Wrox Chapter on Demand product you purchase from this site will come with certain restrictions that allow Wiley to protect the copyrights of its products. After you purchase and download this title, you:
If you have any questions about these restrictions, you may contact Customer Care at (877) 762-2974 (8 a.m. - 5 p.m. EST, Monday - Friday). If you have any issues related to Technical Support, please contact us at 800-762-2974 (United States only) or 317-572-3994 (International) 8 a.m. - 8 p.m. EST, Monday - Friday).
Related Books
Web Resources