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 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.