Changeset 4586

Show
Ignore:
Timestamp:
05/16/08 03:39:02 (2 months ago)
Author:
fredlin
Message:

doc update

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/docs/2.0/Controllers.rst

    r4408 r4586  
    2525 
    2626Suppose using ``tg-admin quickstart`` you generate a TurboGears project named 
    27 "gs". Your default controller code would be created in the file 
    28 ``gs/gs/controllers/root.py``. 
     27"HelloWorld". Your default controller code would be created in the file 
     28``HelloWorld/helloworld/controllers/root.py``. 
    2929 
    3030Modify the default ``controllers.py`` to read as follows: 
     
    3333     
    3434    """Main Controller""" 
    35     from gs.lib.base import BaseController 
     35    from helloworld.lib.base import BaseController 
    3636    from tg import expose, flash 
    3737    from pylons.i18n import ugettext as _ 
    3838    #from tg import redirect, validate 
    39     #from gs.model import DBSession 
     39    #from helloworld.model import DBSession 
    4040 
    4141    class RootController(BaseController): 
     
    5959 
    6060URLs not explicitly mapped to other methods of the controller will generally be  
    61 directed to the method named ``default()`` . With the above example, requesting 
     61directed to the method named ``default()``. With the above example, requesting 
    6262any URL besides ``/index``, for example ``http://localhost:8080/hello``, will  
    6363return the message "This page is not ready".  
     
    8888 
    8989    """Main Controller""" 
    90     from gs.lib.base import BaseController 
     90    from helloworld.lib.base import BaseController 
    9191    from tg import expose, flash 
    9292    from pylons.i18n import ugettext as _ 
    9393    #from tg import redirect, validate 
    94     #from gs.model import DBSession 
     94    #from helloworld.model import DBSession 
    9595 
    9696First you need to import the required modules.  
     
    102102BaseController from the lib module of your own project, and not from TurboGears.  
    103103 
    104 TurboGears provides a base TGController, but it's imported in lib so that you  
    105 can modify it to suit the needs of your appication. For example you 
     104TurboGears provides a base TGController, but it's imported in lib folder of current project (HelloWorld/helloworld/lib).  
     105So that you can modify it to suit the needs of your appication. For example you 
    106106can define actions which will happen on every request, add parameters 
    107107to every template call, and otherwise do what you need to the request on the way 
     
    110110The next thing to notice is that we are importing `expose` from `tg`.   
    111111`BaseController`` classes and the expose decorator are the basis of TurboGears  
    112 controllers.   The @expose decorator declarairs that your method should be  
     112controllers.   The `@expose` decorator declairs that your method should be  
    113113*exposed to the web*, and provides you with the ability to say how the results  
    114 of the controller should be rendered.  
    115  
    116 The other imports are there incase you do internationalization, or use the  
     114of the controller should be rendered. 
     115 
     116The other imports are there incase you do the internationalization, 
     117or use the http redirect function, validate inputs/outputs, or use the models. 
    117118 
    118119:: 
     
    120121    class RootController(BaseController): 
    121122 
    122 The required standard name for the RootController class of a TurboGears  
    123 application is ``RootController`` and it should be inherited from  
     123``RootController`` is the required standard name for the RootController class of a TurboGears application and it should be inherited from  
    124124``BaseController`` class. It is thereby specified as the request handler class  
    125125for the website's root.  
     
    129129class. 
    130130 
    131 We look at the methods of the ``Root`` class next:: 
     131:: 
    132132 
    133133    def index(self):  
     
    137137.. _three urls: 
    138138 
    139 The ``index`` method is the start point of any TurboGears/CherryPy class. When 
    140 you access a URL like  
     139We look at the methods of the ``Root`` class next. 
     140 
     141The ``index`` method is the start point of any TurboGears controller class. When 
     142you access a URL like 
    141143 
    142144* http://localhost:8080  
     
    146148they are all mapped to the ``RootController.index()`` method. 
    147149 
    148 If a URL is requested that does not map to a specific method, the 
     150If a URL is requested and does not map to a specific method, the 
    149151``default()`` method of the controller class is called:: 
    150152 
     
    157159 
    158160As you can see from the examples, the response to a given URL is determined by 
    159 the method it maps to.  
     161the method it maps to. 
    160162 
    161163:: 
     
    163165    @expose() 
    164166 
    165 The ``@expose()`` seen before each controller method directs TurboGears to make 
     167The ``@expose()`` seen before each controller method directs TurboGears controllers to make 
    166168the method accessible through the web server. Methods in the controller class 
    167169that are *not* "exposed" can not be called directly by requesting a URL from the 
     
    171173sophisticated rendering features that we will explore shortly. 
    172174 
    173 Are you sure you wanted to ``expose`` that
    174 --------------------------------------------- 
     175Are you sure you wanted to ``expose`` strings all the time
     176------------------------------------------------------------ 
    175177 
    176178As shown above, controller methods return the data of your website. So far, we 
    177179have returned this data as literal strings. You could produce a whole site by 
    178 returning only strings containing raw HTML from your controller methods but it 
     180returning only strings containing raw HTML from your controller methods, but it 
    179181would be difficult to maintain, since Python code and HTML code would not be 
    180182cleanly separated. 
     
    207209    </html> 
    208210 
    209 By adding a method to the controller like this ... 
     211The ``${param}`` syntax in template means there's some undetermined values need to be filled. 
     212 
     213We did that by adding a method to the controller like this ... 
    210214 
    211215:: 
    212216 
    213     @expose(template="gs.templates.sample") 
     217    @expose(template="helloworld.templates.sample") 
    214218    def example(self):  
    215219        mydata = {'person':'Tony Blair','office':'President'} 
     
    226230* The web user sees a marked up page saying: 
    227231 
    228 **I just want to say that Tony Blair should be the next President  
    229 of the United States.** 
     232The result is:: 
     233 
     234  **I just want to say that Tony Blair should be the next President of the United States.** 
    230235 
    231236Template files can thus house all markup information, maintaining clean 
    232237separation from controller code. 
    233238 
    234 Passing Arguments to the Controller  
    235 --------------------------------------- 
    236  
    237 HTTP get request will have the query parameters turned into a dictionary,  
    238 which is then turned into keyword arguments passed into your controller 
    239 methods. Likewise HTTP POST requests will have the form arguments turned  
    240 into a dictionary which is similarly turned into parameter values  
    241 passed into your controller.  
    242  
    243239SubControllers and the URL Hierarcy 
    244240----------------------------------- 
     
    250246 
    251247To make a sub-controller, all you need to do is make your sub-controller  
    252 inherit from the object class.  However there's a SubController class in  
    253 lib.base for you to use if you want a central place to add helper methods 
    254 or other functionality to your SubControllers:: 
    255  
    256     from lib.base import BaseController, SubController 
     248inherit from the object class.  However there's a SubController class ``Controller`` in  
     249your project's lib.base (HelloWorld/helloworld/lib/base.py) for you to use if you want a central place to add helper methods or other functionality to your SubControllers:: 
     250 
     251    from lib.base import BaseController, Controller 
    257252    from tg import redirect 
    258253 
    259     class MovieController(SubController): 
     254    class MovieController(Controller): 
     255        @expose() 
    260256        def index(self): 
    261257            redirect('list/') 
    262258 
     259        @expose() 
    263260        def list(self): 
    264261            return 'hello' 
     
    267264        movie = MovieController() 
    268265 
    269 Once you've done this you can go to:  
     266Once you've done, you can follow the link:  
    270267 
    271268* http://localhost:8080/movie/  
     
    276273* http://localhost:8080/movie/list/ 
    277274 
    278 Unlike turbogears 1 going to http://localhost:8080/movie **will not** redirect  
     275Unlike turbogears 1, going to http://localhost:8080/movie **will not** redirect  
    279276you to http://localhost:8080/movie/list.  This is due to some interesting bit  
    280277about the way WSGI works.   But it's also the right thing to do from the  
     
    291288exactly where you wanted to go, no matter where you came from.  
    292289 
     290Passing Arguments to the Controller  
     291--------------------------------------- 
     292 
     293HTTP GET request will have the query parameters turned into a dictionary,  
     294which is then turned into keyword arguments passed into your controller 
     295methods. Likewise HTTP POST requests will have the form arguments turned  
     296into a dictionary which is similarly turned into parameter values  
     297passed into your controller.  
     298 
     299When you got the parameters, those parameters are in plain string format. 
     300You should translate those plain strings to some useful format(type) for further process. 
     301TurboGears helps you to translate and validate those parameters by ``validate`` module and  widget framework. But it's the another topic. 
     302 
    293303 
    294304What's new in TG2 
     
    296306 
    297307Here are the major differences in dispatch between CherryPy/Turbogears1  
    298 and TurboGears 2. 
     308and TurboGears 2. 
    299309 
    300310* We have not yet implemented cherrypy's mechanism that replaces dots in the  
     
    307317* Redirect does not know "where you are" in the object tree and move you on  
    308318  from there, it just joins the URL the user requested, with the absolute 
    309   or relative URL you provide.   Using abosolute URLs is recomended.  
     319  or relative URL you provide.   Using abosolute URLs is recommended.  
    310320 
    311321The new TG2 Lookup Method 
    312322-------------------------- 
    313323 
    314 Lookup and default are called in identical situations: when "normal" 
     324``Lookup`` and ``default`` are called in identical situations: when "normal" 
    315325object traversal is not able to find an exposed method, it begins 
    316326popping the stack of "not found" handlers.  If the handler is a 
     
    363373editing hierarchical data (/client/1/project/2/task/3/edit).   
    364374 
    365 The benefit over "default" handlers is that you _return_ an object that acts  
    366 as a sub-controller and continue traversing rather than _being_ a controller  
     375The benefit over "default" handlers is that you *return* an object that acts  
     376as a sub-controller and continue traversing rather than *being* a controller  
    367377and stopping traversal altogether.  This allows you to use actual objects with  
    368378datain your controllers.  
  • trunk/docs/2.0/QuickStart.rst

    r4575 r4586  
    3030Let's go in there and you can take a look around:: 
    3131 
    32   $ cd helloworld 
     32  $ cd HelloWorld 
    3333 
    3434 
  • trunk/docs/2.0/SQLAlchemy.rst

    r4396 r4586  
    7575  >>> dir(types) 
    7676 
     77Data Types 
     78~~~~~~~~~~~ 
     79 
    7780main types are: 
    7881 
     
    9295 
    9396Properties 
    94 ----------- 
     97~~~~~~~~~~~ 
     98 
     99While you define the Columns, you could specify several properties to control the column's behaviors. 
    95100 
    96101============  ========== 
     
    105110--------------------------------- 
    106111 
    107 Once you've got a table, such as the movie_table we're using in this example you can create a Movie class to support a more object oriented way of looking at your data:: 
     112Once you've got a table, such as the movie_table we're using in this example, you can create a Movie class to support a more object oriented way of manipulating your data:: 
    108113 
    109114  class Movie(object): 
     
    112117          self.year = year 
    113118          self.description = description 
    114      
     119 
    115120      def __repr__(self): 
    116121          return "<Movie('%s','%s', '%s')>" % (self.title, self.year, self.description) 
    117122 
    118123 
    119 If you're following along with the tuturial, you'll want to make sure you custom __init__ method.  We'll use this to creae new Movie instances, and set their data all at once througout the rest of the tutorial.  
    120  
    121  
    122 If you don't define the __init__ method. You will need to update the properties of a movie object after it's been created like this:: 
     124If you don't define the __init__ method. You will need to update the properties of a movie object after it's been created. like this:: 
    123125 
    124126  >>> entry = Movie() 
     
    127129  >>> entry.description = 'vampire movie' 
    128130 
    129 But if the __init__ method we defined allows you to initialize the properties at the same time you create the object:: 
     131If you're following along with the tuturial, you'll want to make sure that you've defined the __init__ method.  We'll use the Movie class to creae new Movie instances, and set their data all at once througout the rest of the tutorial. 
     132 
     133If you defined the __init__ method, it allows you to initialize the properties at the same time while you create the object:: 
    130134 
    131135  >>> entry = Movie(title='Dracula', year='1931', description='vampire movie') 
     
    135139  >>> entry = Movie('Dracula', '1931', 'vampire movie') 
    136140 
     141It looks better. 
     142 
     143 
    137144Quick database creation 
    138145-------------------------- 
    139146 
    140 Once you've got your database table objects defined (and imported into __init__.py if you didn't define them there), you can create the tables in the database with one simple command, just run:: 
     147Once you've got your database table objects defined (and imported into __init__.py if you didn't define your model in __init__.py), you can create the tables in the database with one simple command, just run:: 
    141148 
    142149  paster setup-app development.ini 
     
    144151from within your project's home directory.  
    145152 
    146 Pylons defines a setup-app function that paster will connect to the database and create all the tables we've defined.  
     153Pylons (The TurboGears 2 underground framework) defines a setup-app function that paster will connect to the database and create all the tables we've defined.  
    147154 
    148 There is a default database setup defined in development.ini.  So if you just run this it will create a single-file database in your project directory called devdata.db.  If you change your data model, delete this and rerun the setup-app command
     155The default database setup configurations are defined in development.ini. So if you just run the script without modification of development.ini, the script will create a single-file database, which called 'devdata.db', in your project directory. If you change your data model and want to apply the new database, go delete 'devdata.db' and run the 'paster setup-app' command again
    149156 
    150 TurboGears 2 does support database migrations.   But that's another tutorial.  
     157TurboGears 2 does support database migrations. But that's another tutorial.  
    151158 
    152159Reference: