2.2. Creating a Hello World Application
Okay, now that you have TurboGears and SQLite installed (or another database if you chose that route), you are ready to start your first TurboGears application. This is easier than it looks.
TurboGears provides a command-line administrative interface to help you with common development tasks, from creating a new project to opening up a database and creating all the tables necessary to hold your data. But more about all of that later, for now let's just use the tg-admin command to create a new application.
This couldn't be simpler, you just open up a command prompt window and type the following:
You are then asked for the name of your project. For this first project, just call it hello. The next thing you must provide is your package name. TurboGears does its best to turn your project name into a reasonable package name, omitting spaces and other characters that are illegal in package names. So, in this case, you can just press Enter to accept the default package name, which will also be hello. You also want to answer no to the next question because we do not plan to password protect this little "hello world" application.
Enter project name: hello Enter package name: [hello]
Then it spits out a bunch of text like this:
This quickstart command creates a directory structure, and a couple of template files for you to put your code into. Don't worry about the details just yet, however; we come back to it. For now, we just want to get started doing something that works!
You can now navigate into the hello directory that we just created. In it you will find start-hello.py. Run this by entering the following:
You should then see this:
mark@ubuntu:~/tg0.9/hello$ python start-hello.py 2005/12/26 13:03:58 CONFIG INFO Server parameters: 2005/12/26 13:03:58 CONFIG INFO server.environment: development 2005/12/26 13:03:58 CONFIG INFO server.logToScreen: True 2005/12/26 13:03:58 CONFIG INFO server.logFile: 2005/12/26 13:03:58 CONFIG INFO server.protocolVersion: HTTP/1.0 2005/12/26 13:03:58 CONFIG INFO server.socketHost: 2005/12/26 13:03:58 CONFIG INFO server.socketPort: 8080 2005/12/26 13:03:58 CONFIG INFO server.socketFile: 2005/12/26 13:03:58 CONFIG INFO server.reverseDNS: False 2005/12/26 13:03:58 CONFIG INFO server.socketQueueSize: 5 2005/12/26 13:03:58 CONFIG INFO server.threadPool: 0 2005/12/26 13:03:58 HTTP INFO Serving HTTP on http://localhost:8080/
If you get an error, the most likely cause is that you already have a service running on port 8080. You can fix this easily by running TurboGears on another port. To change the port your TurboGears application runs on, just open dev.cfg in the root directory of your application and change the line that reads
(Remember to remove the # sign!!!)
At this point, you should be able to run the start-hello.py program.
If everything works, you should next be able to open a browser and go to http://localhost:8080/ and see the "Are you ready to Gear Up?" page (as shown in Figure 2.1). Of course, if you are running on another port, you must put that in the URL!!
Figure 2.1. TurboGears welcome page
If this worked, your application is up and running.