将自定义python函数传递到龙卷风模板
发布时间:2020-12-20 13:03:13 所属栏目:Python 来源:网络整理
导读:我想编写一个自定义函数并将其传递给我的龙卷风模板. 像def trimString(data):返回数据[0:20]然后将其推入我的龙卷风文件. 这应该允许我修剪字符串. 这可能吗? 谢谢. 解决方法 它不是 especially clear in the documentation,但您可以通过在模块中定义此函
我想编写一个自定义函数并将其传递给我的龙卷风模板.
像def trimString(data):返回数据[0:20]然后将其推入我的龙卷风文件. 这可能吗? 谢谢. 解决方法
它不是
especially clear in the documentation,但您可以通过在模块中定义此函数并将模块作为ui_methods参数传递给tornado.web.Application来轻松完成此操作.
I.: 在ui_methods.py中: def trim_string(data): return data[0:20] 在app.py中: import tornado.ioloop import tornado.web import ui_methods class MainHandler(tornado.web.RequestHandler): def get(self): self.render("main.html") urls = [(r"/",MainHandler)] application = tornado.web.Application(urls,ui_methods=ui_methods) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start() 在main.html中: .... {{ trim_string('a string that is too long............') }} .... Andy Boot的解决方案也可以使用,但是在每个模板中自动访问这样的功能通常很不错. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Python str与Unicode的编码处理区别用法
- aws 预留实例到期监控
- Python BeautifulSoup中文乱码问题的2种解决方法
- ansible动态Inventory主机清单配置遇到的坑
- python – 如何在日期时间字段中按时间过滤?
- python – 强制pyplot.imshow()生成更高分辨率的图像
- ipython-notebook – 通过django_extensions使用时设置IPyt
- python自动查询12306余票并发送邮箱提醒脚本
- python中__call__内置函数用法实例
- 如何使用Python更改域用户(Windows Active Directory)的密码