The following will cover how to work with SQLAlchemy and extend the sample application to log and retrieve a list of greeted people. First we will need to enable SQLAlchemy support for our application:. Now TurboGears will configure a SQLAlchemy engine for us, but it will require that we provide a data model, otherwise it will just crash when starting up.
This can be done by providing a database Session and a model initialization function:. To start working with tables and the data they contain we need to declare the table itself, this can be done through the SQLAlchemy declarative layer by using a Declarative Base class:. This will allow us to read and write rows from the logs table, but before we are able to do so we must ensure that the table actually exists, which can be done by extending our model initialization function to create the tables:.
Now we can finally extend our controller to log the people we greet and provide us the list of past greetings:. The Full Stack Tutorial provides an introduction to more complex applications with all the TurboGears features enabled, follow it if you want to unleash all the features that TurboGears provides! Navigation index modules next previous TurboGears 2. Note While minimal mode is well suited for small simple web applications or web services, for more complex projects moving to a package based configuration is suggested.
Note How and why requests are routed to the index and hello methods is explained in Object Dispatch documentation. The setup. If you have just installed TurboGears and are in a relatively new virtualenv, expect to see a bit of output about additional packages being installed. Before you run your application for the first time, you need to make sure the database is created and initialized.
The following command typically only needs to be run once. By default, the database is created using SQLite , and the data is stored in a file, devdata. The information about what database driver is used is specified in the development. This allows you to use setup-app to create database tables, pre-populate require data into your database, and otherwise make things nice for people first setting up your app.
Inside of this, you may do any setup you need to for your application. The most common operations will be to add in basic data to the database that is required to bootstrap your application. The tg. If you need to perform startup time setup based on the supplied configuration, you should do so in a milestone or in an hook. Milestones are available through the tg. Registering an action to be executed whenever a milestone is reach can be done using tg.
The registered action takes no parameters. Milestones are much like Hooks but they are only related to the configuration process. The major difference is that while an hook can fire multiple times a milestone can be reached only once. The order of execution of the milestones and hooks provided during the application startup process is:. A good indicator of whether an option should be set in the config directory code vs.
If the option should be changed depending on deployment, it belongs in the ini files. You can easily customize it to your needs. You can refer to FullStackApplicationConfigurator documentation for the list of components enabled by default. The configurator object comes with a bunch of preregistered components that automate the majority of what you need to do. These shortcuts eliminate the need to provide your own setup methods for configuring your TurboGears application.
To see the list of available configuration options refer to Configuration Options. You might want to replace behaviours of your application or add new components that are not available in TurboGears by default. Registering new components is done through the FullStackApplicationConfigurator. This sets up a Python 2. The use of —no-site-packages prevents conflicts with any packages installed into the platform directories. See VirtualEnv for details on VirtualEnv.
Please note we are using tg2env as the name of the virtual environment. This command installs TurboGears 2. Your projects will normally use a particular version of TurboGears. A large number of packages will be installed. These are the officially required packages which define TurboGears itself. The following command will install your new package into your VirtualEnv and will download a number of packages which are not technically part of TurboGears, but which provide useful features for the quick-started application.
0コメント