- TurboGears - Deployment
- TurboGears - Restful Applications
- TurboGears - Pluggable Applications
- TurboGears - Writing Extensions
- TurboGears - Hooks
- TurboGears - Scaffolding
- TurboGears - Using MongoDB
- Authorization & Authentication
- TurboGears - Admin Access
- TurboGears - Pagination
- TurboGears - DataGrid
- TurboGears - Crud Operations
- TurboGears - Creating Models
- TurboGears - Sqlalchemy
- TurboGears - Caching
- TurboGears - Cookies and Sessions
- TurboGears - Flash Messages
- TurboGears - Validation
- TurboGears - Toscawidgets Forms
- TurboGears - URL Hierarchy
- TurboGears - JSON Rendering
- TurboGears - Includes
- Genshi Template Language
- TurboGears - HTTP Methods
- TurboGears - Serving Templates
- TurboGears - Dependencies
- TurboGears - First Program
- TurboGears - Environment
- TurboGears - Overview
- TurboGears - Home
TurboGears Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
TurboGears – Scaffolding
Gearbox toolkit contains scaffold command, which is very useful to quickly create new components of TurboGears apppcation. An apppcation generated by quickstart command of gearbox has a skeleton template in the model folder (model.py.template), a templates folder (template.html.template) and a controllers folder (controller.py.template). These ‘.template’ files are used as basis for creating new scaffolds for an apppcation
For example, in order to create a new model named mymodel, simply run the following command −
gearbox scaffold model mymodel
This command will generate model/mymodel.py with newmodel class defined in it.
# -*- coding: utf-8 -*- """Mymodel model module.""" from sqlalchemy import * from sqlalchemy import Table, ForeignKey, Column from sqlalchemy.types import Integer, Unicode, DateTime, LargeBinary from sqlalchemy.orm import relationship, backref from hello.model import DeclarativeBase, metadata, DBSession class Mymodel(DeclarativeBase): __tablename__ = mymodels uid = Column(Integer, primary_key = True) data = Column(Unicode(255), nullable = False) user_id = Column(Integer, ForeignKey( tg_user.user_id ), index = True) user = relationship( User , usepst = False, backref = backref( mymodels ,cascade = all, delete-orphan )) __all__ = [ Mymodel ]
The users can now make modifications in the table structure as per their requirement and then import it inside model/__init__.py to make the model available inside the apppcation.
In order to create a model, a controller class to handle it and an index page all these three components can be created simultaneously by the following command.
gearbox scaffold model controller template mymodel
This command will result in controllersmymodel.py in which the MymodelController class is duly defined.
# -*- coding: utf-8 -*- """Mymodel controller module""" from tg import expose, redirect, vapdate, flash, url # from tg.i18n import ugettext as _ # from tg import predicates from hello.pb.base import BaseController # from hello.model import DBSession class MymodelController(BaseController): # Uncomment this pne if your controller requires an authenticated user # allow_only = predicates.not_anonymous() @expose( hello.templates.mymodel ) def index(self, **kw): return dict(page = mymodel-index )
To start using this controller, mount it inside your apppcation RootController just to define an instance of MymodelController. Add these pnes in the controllers oot.py −
From hello.controller.mymodel import MymodelController class RootController(BaseController): mymodel = MymodelController()
A template scaffold templatesmymodel.html will also be created in the templates folder. It will act as an index page for ‘/mymodel’ URL.
The generated mymodel.html file in the templates folder will be as follows −
<html xmlns = "http://www.w3.org/1999/xhtml" xmlns:py = "http://genshi.edgewall.org/" xmlns:xi = "http://www.w3.org/2001/XInclude"> <xi:include href = "master.html" /> <head> <title>Mymodel</title> </head> <body> <span class = "row"> <span class = "col-md-12"> <h2>Mymodel</h2> <p>Template page for Mymodel</p> </span> </span> </body> </html>Advertisements