One month ago, I gave an introductory speech about TurboGears at the Israeli Pythoneers Meeting. The discussion consisted of two parts. The first part introduced TurboGears, and the second part included live aucasinosonline coding of a Flickr clone.
Because it was only the second time I had attended one of these meetings, I briefly introduced myself. The two slides above show photos that were taken in my home town, Adi, which is a small town in Northern Israel. In the right photo, you can see my cat Uzi watching the cows in our backyard. If you have taken part in the Python Challenge, you might have already met Uzi.
TurboGears is a framework that enables the rapid development of Web applications in Python. But what is a Web application? Great question!
Wikipedia provides this explanation:
In software engineering, a Web application or webapp is an application that is accessed with a Web browser over a network such as the Internet or an intranet.
Web applications are popular due to the ubiquity of the browser as a client, sometimes called a thin client. The ability to update and maintain Web applications without distributing and installing software on potentially thousands of client computers is a key reason for their popularity. Web applications are used to implement Webmail, online retail sales, online auctions, wikis, discussion boards, Weblogs, MMORPGs, and many other functions.
Why should you choose TurboGears as your Web-application development framework for your next project? Well, the short answer is that PHP is the devil â€“ it tempts you to mix HTML, logic, and database queries into the same piece of code. Rails, as great as it may be, is written in Ruby, and we like Python better. For Django, I borrowed (err, stole) Adrian Holovaty’s port of Mark Pilgrim’s piece:
I was walking across a bridge one day, and I saw a man standing on the edge, about to jump off. So I ran over and said, â€œStop! Donâ€™t do it!â€
â€œI canâ€™t help it,â€ he cried. â€œIâ€™ve lost my will to live.â€
â€œWhat do you do for a living?â€ I asked.
He said, â€œI work with computers.â€
â€œMe too!â€ I said. â€œWhat do you do with computers?â€
He said, â€œI’m a Web developer.â€
â€œMe too!â€ I said. â€œDesign, client-side programming or server-side programming?â€
He said, â€œServer-side programming.â€
â€œMe too!â€ I said. â€œDo you use dynamically typed languages or statically typed languages?”
He said, â€œDynamically typed languages.â€
â€œMe too!â€ I said. â€œDo you use a Web framework, or do you roll things on your own?â€
He said, â€œI use a Web framework.â€
“Me too!â€ I said. â€œTurboGears or Django?â€
He said, â€œDjango.â€
â€œDie, heretic scum!â€ I shouted, and I pushed him over the edge.
The moral of the story is that TurboGears and Django have much more in common than they have in the way of differences. It has been said that Django is like a Macintoshâ€”it simply worksâ€”while TurboGears is more like Unixâ€”its power comes from its being made of many smaller building blocks, each one of them designed to do one thing well.
TurboGears and Django try to solve different types of problems. Django was born in a publishersâ€™ newsroom environment, where Web applications needed to be launched within hours. By its very nature, Django is geared toward the development of content-oriented Web sites. TurboGears, on the other hand, evolved while Kevin Dangoor was developing Zesty News, a news-reader application. As a result, it is more suitable for Web applications (see earlier definition).
Having said that, I found it very easy to develop content-oriented Web sites in TurboGears. Currently, TurboGearsâ€™ CRUD features and administration panel are somewhat lacking in comparison to Djangoâ€™s. As such you have to manually code these aspects of your application if you want a non-tech author to be able to fill your projectâ€™s database with content or change various aspects of the Web site.
TurboGears goes a long way in encouraging you to design your application in the so-called MVC paradigm. MVC stands for Model, Viewer, and Controller. The model consists of the classes that represent the information on which the application operates. That information is usually stored in an SQL database. The view is composed of the HTML page templates that are used to render the model. The controller responds to user input (HTTP requests) by operating on the model and returning a view for subsequent interaction.
In a nutshell, MVC is all about separating metadata, logic, and presentation.
The above is an example of how you would model a user with SQLObject. It has first_name and last_name fields as well as a date-time field, which represents the time at which the user was last logged in to the system.
The above class definition can automatically generate the SQL table creation statement:
The â€œPersonâ€ class gives you a very comfortable interface with the database objects:
In the next installment, I will show you how easy it is to create a Flickr killer application with TurboGears in 37 minutes.
Make sure you won’t miss it by subscribing to the RSS feed.