首页 > 其他 > 详细

Tornado StaticFileHandler/static_path/template_path 笔记

时间:2018-11-28 21:43:23      阅读:295      评论:0      收藏:0      [点我收藏+]

使用StaticFileHandler进行首页默认访问页面,最好将StaticFileHandler放在最后面,这样不会覆盖要匹配自定义的路径

import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
from tornado.options import options
from tornado.web import RequestHandler, StaticFileHandler
import os

current_path = os.path.dirname(__file__)
tornado.options.define(port, type=int, default=8000, help="服务器端口")

class IndexHandler(RequestHandler):
    def get(self):
        self.write(OK)

if __name__ == __main__:
    tornado.options.parse_command_line()
    app = tornado.web.Application([
        (r/(.*), StaticFileHandler, dict(path=os.path.join(current_path, static/html), default_filename=index.html)),
    ], debug=True)
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

 

static_path/template_path

static_path:设置静态文件的访问目录

template_path:设置静态页面路径

static_url(): 根据设置的静态,目录寻找静态文件

render():跳转文件

import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
from tornado.options import options
from tornado.web import RequestHandler, StaticFileHandler
import os

current_path = os.path.dirname(__file__)
tornado.options.define(port, type=int, default=8000, help="服务器端口")

class IndexHandler(RequestHandler):
    def get(self):
        # self.render(‘index.html‘)  # 跳转静态页面
        dict1 = {name: namejr, age:22}
        self.render(index.html,dict1=dict1)  # 使用render()还可以传递参数

if __name__ == __main__:
    tornado.options.parse_command_line()
    app = tornado.web.Application([
        (r/, IndexHandler),
        # 使用StaticFileHandler进行首页默认访问页面,最好将StaticFileHandler放在最后面,这样不会覆盖要匹配自定义的路径
        (r/(.*), StaticFileHandler, dict(path=os.path.join(current_path, static/html), default_filename=index.html)),
    ], debug=True, static_path=os.path.join(current_path, static), template_path=os.path.join(current_path, templates))
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

 

使用render()传递参数接收方法:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <!-- static_url使用静态资源文件 -->
    <link rel="stylesheet" type="text/css" href="{{ static_url(‘css/index.css‘) }}">
</head>
<body>
<h1>namejr</h1>
<p>name :{{ dict1[name] }}, age:{{ dict1[age] }}</p>
</body>
</html>

 

关于静态文件使用if..else..等语句

{% if %}...{% elif %}..{% else %}...{% end %}

{% for %} {% end %}

即使用{%%}方式执行python语句,使用{{ num }} 接收静态参数

Tornado StaticFileHandler/static_path/template_path 笔记

原文:https://www.cnblogs.com/namejr/p/10034498.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!