查看文章 |
Turbogears is a rapid web development frame work, which I've been using for a while now. Unlike Django, which is pretty buggy, Turbogears offers a more stable environment. And it truly makes web development easier. In this tutorial I'll show you how to build your own blog with turbogears in 2 hours. First thing first, you have to make sure python and turbogears are all properly installed and configured on your computer. There is a very detailed installation guide here so I'm not gonna explain this in details this time. The next step is to create the project using turbogears' administration tool - quickstart. It will create the project template for us. Let's just name our project 'blog'. To create the project folder, open your cmd window or terminal window, and type: "tg-admin quickstart blog" You'll be asked to enter the package name, which is 'blog' of course, and whether you need Identity control in this project. Turbogears offers a well-written identity control package, which have saved you a lot of time. But we'll not use it at this time. The quickstart command generates a bunch of folders and files like this:
To start your website, enter the blog folder, and type "python start-blog.py". By default, the server listens to the 8080 port of the localhost. Open a browser and navigate to http://localhost:8080/ and you should be able to see the default welcome page of turbogears telling you that "your application is now running": Next, we'll probably have to determine the data structure of our blog. By default, Turbogears uses SQLObject to handle the database objects, which saves you from writing complicated sql commands. And the database models are defined in model.py. First, every blog is belonged to a category, for example, 'Funny','IT', etc. So we'll add a table called Category first: class Category(SQLObject): MutipleJoin indicates that there could be a number of blogs belonging to one category. then Blog: class Blog(SQLObject): ForeignKey('Category') indicates that this blog belongs to a category. Comments: class Comment(SQLObject): Please refer to the SQLObject doc for grammar details. I think the grammar is pretty self-explonatory. After we've created these 3 database object, the next step is to actually create it in our local database. Take mysql for example. First, find dev.cfg in our project directory and open it. Comment 'sqlobject.dburi="sqlite://%(current_dir_uri)s/devdata.sqlite"' by adding a '# '(a blank space is needed after the #), and then uncomment line 11 (or the line with 'mysql:' in), modify it to your mysql setting, e.g.: sqlobject.dburi="mysql://root:@127.0.0.1:3306/blog" this means that we are using the mysql server at localhost, port 3306, with the user root and no password, and the database named 'blog'. Then add the following lines to the very beginning of model.py: from sqlobject import * con = "mysql://root:@127.0.0.1:3306/blog" Now we are ready to create the tables in our database. Stop the server(by hitting Ctrl+C in the previous cmd window where we started the web server) and type: tg-admin sql create and the tables are created.
Notice that MultipleJoin does not actually create a column in the table, while ForeignKey creates the blog_id and category_id columns. By far we have successfully set up the evironment and are ready to rock. If the evironment is already set up for use, creating the project folder / configure the config file / creating tables in the database wouldn't take us more than 5 minutes. In the next post I'll show you how to create pages for our blog. (I'm not a native English speaker so forgive me if there's any grammar mistakes.) |


