TurboGears 2.1工程目录详解
2010-11-03 21:00
246 查看
在使用paster quickstart命令创建了TurboGears 2.1工程目录后,让我们来看quickstart为我们创建了哪些文件和目录。
从上面的图里面可以看到,quickstart命令只创建了很少的文件。如果你挑两三个文件看一下内容,你会看到里面主要还是应用程序的框架,你可以在框架中填充代码构造你自己的web应用。
在helloworld/controllers目录中你会看到很多的控制器文件。
root.py 是我们第一个要介绍的文件,这个文件默认会接受所有的HTTP请求,然后根据请求的内容调用相应的函数。你可以通过设置特定的路由修改默认设置,不过通常情况下不需要这么做。
控制器的函数
控制器是你应用程序的神经中枢。他除了了所有的HTTP请求。控制器相应请求之后会调用其他的TurboGears组件,包括模板引擎、存储层等,来完成请求的响应。
When the TurboGears server receives an HTTP request, the requested URL is mapped as a call to your controller code located in controllers.py. Page names map to functions within the controller class.
For example:
Quick Example
Here’s a simple example of the TG2.
Suppose using tg-admin quickstart you generate a TurboGears project named “HelloWorld”. Your default controller code would be created in the fileHelloWorld/helloworld/controllers/root.py.
Modify the default controllers.py to read as follows:
When you load the root URL http://localhost:8080/index in your web browser, you’ll see a page with the message “Hello World” on it.
root.py
Let’s take a look at the RootController:
There are a couple obvious differences from the simplistic example above:
Most of the expose() calls point to a specific template file.
We mount the SecureController, AdminController, etc in secc, admin, by instantiating them in RootController
从上面的图里面可以看到,quickstart命令只创建了很少的文件。如果你挑两三个文件看一下内容,你会看到里面主要还是应用程序的框架,你可以在框架中填充代码构造你自己的web应用。
1、控制器
当浏览器(或者其他的HTTP客户端)向你的应用发出请求的时候控制器会被调用。在helloworld/controllers目录中你会看到很多的控制器文件。
root.py 是我们第一个要介绍的文件,这个文件默认会接受所有的HTTP请求,然后根据请求的内容调用相应的函数。你可以通过设置特定的路由修改默认设置,不过通常情况下不需要这么做。
控制器的函数
控制器是你应用程序的神经中枢。他除了了所有的HTTP请求。控制器相应请求之后会调用其他的TurboGears组件,包括模板引擎、存储层等,来完成请求的响应。
When the TurboGears server receives an HTTP request, the requested URL is mapped as a call to your controller code located in controllers.py. Page names map to functions within the controller class.
For example:
URL | Maps to |
http://localhost:8080/index | Root.index() |
http://localhost:8080/mypage | Root.mypage() |
Here’s a simple example of the TG2.
Suppose using tg-admin quickstart you generate a TurboGears project named “HelloWorld”. Your default controller code would be created in the fileHelloWorld/helloworld/controllers/root.py.
Modify the default controllers.py to read as follows:
"""Main Controller""" from helloworld.lib.base import BaseController from tg import expose, flash from pylons.i18n import ugettext as _ #from tg import redirect, validate #from helloworld.model import DBSession class RootController(BaseController): @expose() def index(self): return "<h1>Hello World</h1>" @expose() def default(self, *args, **kw): return "This page is not ready"
When you load the root URL http://localhost:8080/index in your web browser, you’ll see a page with the message “Hello World” on it.
root.py
Let’s take a look at the RootController:
class RootController(BaseController): """ The root controller for the my-intranet application. All the other controllers and WSGI applications should be mounted on this controller. For example:: panel = ControlPanelController() another_app = AnotherWSGIApplication() Keep in mind that WSGI applications shouldn't be mounted directly: They must be wrapped around with :class:`tg.controllers.WSGIAppController`. """ secc = SecureController() admin = Catwalk(model, DBSession) error = ErrorController() @expose('my_intranet.templates.index') def index(self): """Handle the front-page.""" return dict(page='index') @expose('my_intranet.templates.about') def about(self): """Handle the 'about' page.""" return dict(page='about') @expose('my_intranet.templates.authentication') def auth(self): """Display some information about auth* on this application.""" return dict(page='auth') @expose('my_intranet.templates.index') @require(predicates.has_permission('manage', msg=l_('Only for managers'))) def manage_permission_only(self, **kw): """Illustrate how a page for managers only works.""" return dict(page='managers stuff') @expose('my_intranet.templates.index') @require(predicates.is_user('editor', msg=l_('Only for the editor'))) def editor_user_only(self, **kw): """Illustrate how a page exclusive for the editor works.""" return dict(page='editor stuff') @expose('my_intranet.templates.login') def login(self, came_from=url('/')): """Start the user login.""" login_counter = request.environ['repoze.who.logins'] if login_counter > 0: flash(_('Wrong credentials'), 'warning') return dict(page='login', login_counter=str(login_counter), came_from=came_from) @expose() def post_login(self, came_from=url('/')): """ Redirect the user to the initially requested page on successful authentication or redirect her back to the login page if login failed. """ if not request.identity: login_counter = request.environ['repoze.who.logins'] + 1 redirect(url('/login', came_from=came_from, __logins=login_counter)) userid = request.identity['repoze.who.userid'] flash(_('Welcome back, %s!') % userid) redirect(came_from) @expose() def post_logout(self, came_from=url('/')): """ Redirect the user to the initially requested page on logout and say goodbye as well. """ flash(_('We hope to see you soon!')) redirect(came_from)
There are a couple obvious differences from the simplistic example above:
Most of the expose() calls point to a specific template file.
We mount the SecureController, AdminController, etc in secc, admin, by instantiating them in RootController
相关文章推荐
- Android 2.1 和 Android 4.4 工程目录超详细对比及详解
- Android项目工程目录详解
- Android工程的目录结构详解
- 详解Node.js包的工程目录与NPM包管理器的使用
- rails工程目录详解
- 微信小程序开发详解(三)---微信小程序工程目录结构
- Maven详解(三)------ Maven工程目录介绍
- Android开发二:工程目录结构详解
- Android学习笔记(6)---Android 2.3.3 工程目录详解
- Android工程的目录结构详解
- Android工程的目录结构详解
- Eclipse工程目录详解By荼荼小蘼
- Android工程的目录结构详解
- Eclipse:java工程目录详解
- android studio 工程目录详解
- Eclipse ADT 创建Android项目----工程目录详解