使用 RequestHandler.render 方法支持缺省首页 index.html
2012-04-05 20:48
316 查看
印象中刚接触 PHP 的时候,会配置 Apache 支持默认的首页,可以通过 /、/index.html、/index.htm
来访问,这些最终访问的是同一个页面。这里可以通过一个简单的方法来做:
然后传递给对应的 Handler 的 get 方法,因此 get 方法里面需要有对应的占位符。如果没有这个额外的
参数,则会产生不匹配,出现异常。不过,这里使用了直接读取 index.html 的方式,显得有些太原始了,
需要改进一下。
RequestHandler 里面定义了一个 render 方法,可以填充模板生成页面:
访问可以了。使用 /、/index.html、/index.htm 访问的都是同一个页面。
Tornado 文档中还提供了如何支持 static 资源的方法,暂时还没有用到,以后再尝试一下。
来访问,这些最终访问的是同一个页面。这里可以通过一个简单的方法来做:
class IndexHandler(tornado.web.RequestHandler): def get(self, *index): self.write(open("index.html").read()) application = tornado.web.Application([ (r'/(index.html|index.htm)?', IndexHandler), ])注意,IndexHandler 的 get 方法的参数 *index,因为在 url 中使用了正则表达式的(),而()会捕捉表达式
然后传递给对应的 Handler 的 get 方法,因此 get 方法里面需要有对应的占位符。如果没有这个额外的
参数,则会产生不匹配,出现异常。不过,这里使用了直接读取 index.html 的方式,显得有些太原始了,
需要改进一下。
RequestHandler 里面定义了一个 render 方法,可以填充模板生成页面:
render(self, template_name, **kwargs) unbound tornado.web.RequestHandler method Renders the template with the given arguments as the response.但是需要指定模板名字和一个字典参数。这里使用了简单的 index.html,没有任何需要填充的东西。
class IndexHandler(tornado.web.RequestHandler): def get(self, *index): self.render('index.html')
访问可以了。使用 /、/index.html、/index.htm 访问的都是同一个页面。
Tornado 文档中还提供了如何支持 static 资源的方法,暂时还没有用到,以后再尝试一下。
相关文章推荐
- DedeCMS访问网站首页会自动在域名后面多出index.html的解决方法
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- dedecms中去除首页index.html的方法
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- tcp proxy,可以在request_handler或者response_handler进行特殊修改,渗透测试或者协议故障测试的时候可以使用。要想支持ssl,只需connect处进行修改调整
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- AsyncHttpClient, RequestParams ,AsyncHttpResponseHandler三个类使用方法
- DedeCMS首页网址中去掉带index.html的解决方法
- dedecms如何去掉首页index.html的方法
- AsyncHttpClient, RequestParams ,AsyncHttpResponseHandler三个类使用方法
- 编辑器使用方法 1. 下载编辑器 下载 KindEditor 最新版本,下载之后打开 examples/index.html 就可以看到演示。 下载页面: http://www.kindsoft.
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- AsyncHttpClient, RequestParams ,AsyncHttpResponseHandler三个类使用方法
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- 在vue中,v-for的索引index在html中的使用方法
- autoindex使用方法举例说明