tornado 静态文件路径绑定细节

先上web模块项目结构图:
项目树形图


首先要将/static(注意,只能以static,不能带s)和/templates文件夹绑定到tornado.web.Application当中,这样才能让服务器加载渲染html、css、JavaScript等项目文件,具体实现代码如下:

	def make_app():
		setting = dict(
        	template_path=os.path.join(os.path.dirname(__file__), "templates"),
        	static_path=os.path.join(os.path.dirname(__file__), "static"),
    	)
    	return tornado.web.Application([(r'/Demo', DemoHandler),
                                    ],
                                   **setting
                                   )

然后修改html当中对css、js文件的引入方式,一定要多加一对引号啊!!!

以bootstrap.min.css文件为例
错误写法示例:

  • <link rel="stylesheet" type="text/css" href="{{static_url(css/bootstrap.min.css)}}">
    这里很容易忽略,因为当你写成下面这种形式:

  • <link rel="stylesheet" type="text/css" href="{{"static_url(css/bootstrap.min.css"}}">
    IDE会提示错误(实际也没错),所以建议最好用’和"交替的形式,如下:

  • <link rel="stylesheet" type="text/css" href="{{static_url('css/bootstrap.min.css')}}">


后来想想,觉得我这种错误很低级,如果把static_url当作方法,传入的变量本来就不能直接以bootstrap.min.css这种直接传,好歹变成字符串吧…用单引号和双引号都算。
既然花了我这么久才发现这个问题,有必要记录一下!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Tornado是一个Python的Web框架,用于开发高性能、可扩展的Web应用程序。在Tornado中,静态文件与重向是非常常见的功能。下面是对它们的使用说明: 1. 静态文件: 在Tornado中,可以使用`StaticFileHandler`处理静态文件。可以通过以下方式来创建一个`StaticFileHandler`: ```python import tornado.web class StaticHandler(tornado.web.StaticFileHandler): pass ``` `StaticFileHandler`的默认路径是`./static`,可以通过设置`StaticFileHandler`的`path`属性来指静态文件路径。例如: ```python import tornado.web class StaticHandler(tornado.web.StaticFileHandler): def initialize(self, path): super().initialize(path=path) ``` 在使用`StaticFileHandler`时,需要指静态文件的URL和文件路径。可以通过以下方式来指: ```python import tornado.web class StaticHandler(tornado.web.StaticFileHandler): def initialize(self, path): super().initialize(path=path, default_filename='index.html') def get(self): return super().get(self.request.path[1:]) ``` 上面的代码中,`default_filename`指了默认返回的文件名,如果URL中没有指具体的文件名,则返回该文件。`get()`方法实际上是调用了`StaticFileHandler`的`get()`方法,`self.request.path[1:]`是指去掉URL中的第一个字符(即'/')后的字符串。例如,当URL为`http://localhost:8000/static/css/style.css`时,`self.request.path[1:]`的值为`static/css/style.css`。 2. 重向: 在Tornado中,可以使用`RedirectHandler`来进行重向。可以通过以下方式来创建一个`RedirectHandler`: ```python import tornado.web class RedirectHandler(tornado.web.RedirectHandler): pass ``` `RedirectHandler`的默认代码是302,可以通过设置`RedirectHandler`的`code`属性来指向的代码。例如: ```python import tornado.web class RedirectHandler(tornado.web.RedirectHandler): def initialize(self, url): super().initialize(url=url, code=301) ``` 在使用`RedirectHandler`时,需要指向的URL。可以通过以下方式来指: ```python import tornado.web class RedirectHandler(tornado.web.RedirectHandler): def initialize(self, url): super().initialize(url=url, permanent=True) def get(self): return super().get(self.request.path[1:]) ``` 上面的代码中,`permanent`指了是否永久重向。`get()`方法实际上是调用了`RedirectHandler`的`get()`方法,`self.request.path[1:]`是指去掉URL中的第一个字符(即'/')后的字符串。例如,当URL为`http://localhost:8000/old`时,`self.request.path[1:]`的值为`old`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值