月度归档:2013年01月

Pylons web框架的首页问题

对于新上手pylons的朋友来说,网站首页在哪里,如何设置网站首页,是个很大的问题。因为pylons使用了路径路由的方式,写在代码中,而不是像传统的使用文档系统路径的方法。

简单来说,pylons有两种写首页的方法,一种是静态首页,在static目录里,直接放置index.html文件

第二种方法,则是在config/routing.py路由文件中,映射某个路由为首页,比如我们测试文档写的是hello/index,浏览器里是:http://example/hello/index,在路由文件中写入下面的第一行:

map.connect('/', controller='hello', action='index')
map.connect('/{controller}/{action}')
map.connect('/{controller}/{action}/{id}')

那么再浏览 http://example/ 就显示的是http://example/hello/index页面了。

文档参考:

http://pylonsbook.com/en/1.1/exploring-pylons.html

One very common requirement is the ability to map the root URL http://localhost:5000/ to a controller action. After all, you wouldn’t want to be limited to having a static file as the root URL. To do this, you need to add a line to the routes configuration. Add a new line to the top of the main route map in helloworld/config/routing.py just after the # CUSTOM ROUTES HEREcomment so that the routes are defined like this:

# CUSTOM ROUTES HERE

map.connect('/', controller='hello', action='index')
map.connect('/{controller}/{action}')
map.connect('/{controller}/{action}/{id}')

This tells Routes that the root URL / should be mapped to the index action of HelloController. Otherwise, Routes should look for URLs in the form /controller/action/, and /controller/action/id. This, along with other details of Routes, is described in detail in Chapter 9.

 

我记得pyramid框架的首页也类似这样的,稍后再补上。而且pyramid的静态页面处理路径好像还不如pylons呢,唉!