为Python的Tornado框架配置使用Jinja2模板引擎的方法
作者:cangmean 时间:2022-07-19 03:49:07
tornado 默认有一个模板引擎但是功能简单(其实我能用到的都差不多)使用起来颇为麻烦, 而jinja2语法与django模板相似所以决定使用他.
下载jinja2
还是用pip 下载(用的真是爽)
pip install jinja2
这样就可以使用了.
tornado与jinja2 整合
tornado和jinja2整合起来很简单(其实是网上找的比较简单), 不知道从那里找到的反正找到了,不说了直接上代码
#coding:utf-8
import tornado.web
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
class TemplateRendring(object):
"""
A simple class to hold methods for rendering templates.
"""
def render_template(self, template_name, **kwargs):
template_dirs = []
if self.settings.get('template_path', ''):
template_dirs.append(self.settings['template_path'])
env = Environment(loader=FileSystemLoader(template_dirs))
try:
template = env.get_template(template_name)
except TemplateNotFound:
raise TemplateNotFound(template_name)
content = template.render(kwargs)
return content
# 就是重新写 BaseHandler 由jinja2模板渲染
class BaseHandler(tornado.web.RequestHandler, TemplateRendering):
"""
Tornado RequestHandler subclass.
"""
def initialize(self):
pass
def get_current_user(self):
user = self.get_secure_cookie('user')
return user if user else None
def render_html(self, template_name, **kwargs):
kwargs.update({
'settings': self.settings,
'STATIC_URL': self.settings.get('static_url_prefix', '/static/'),
'request': self.request,
'current_user': self.current_user,
'xsrf_token': self.xsrf_token,
'xsrf_form_html': self.xsrf_form_html,
})
content = self.render_template(template_name, **kwargs)
self.write(content)
这样就可以在tornado中替换self.render为self.render_html了.
标签:Tornado,Python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php中实现记住密码自动登录的代码
2023-11-14 18:36:14
从一个项目中来看三层架构
2008-08-06 12:50:00
Python基于list的append和pop方法实现堆栈与队列功能示例
2022-10-20 02:26:26
![](https://img.aspxhome.com/file/2023/4/69014_0s.jpg)
Python给你的头像加上圣诞帽
2023-08-28 11:30:41
![](https://img.aspxhome.com/file/2023/5/92305_0s.jpg)
新年伊始,看各大网站新春LOGO
2009-01-01 19:35:00
![](https://img.aspxhome.com/file/UploadPic/20091/1/114040f01010p00001031-53s.gif)
Python 使用csv库处理CSV文件的方法
2023-03-16 21:37:24
![](https://img.aspxhome.com/file/2023/9/65529_0s.png)
楼层数横排比竖排好
2008-04-26 07:28:00
![](https://img.aspxhome.com/file/UploadPic/20084/26/200842673425574s.jpg)
使用Numpy读取CSV文件,并进行行列删除的操作方法
2023-05-05 03:26:11
Python 实现打印单词的菱形字符图案
2021-06-30 20:08:52
![](https://img.aspxhome.com/file/2023/8/93528_0s.jpg)
修改Linux下MySQL 5.0的默认连接数
2009-09-01 10:16:00
JS获取对象代码总结
2011-03-07 16:14:00
python实现根据图标提取分类应用程序实例
2022-07-23 16:22:39
python3 dict ndarray 存成json,并保留原数据精度的实例
2021-03-04 13:25:31
SQL Server数据库搭建农村信息化的方案
2009-01-23 14:16:00
![](https://img.aspxhome.com/file/UploadPic/20091/2009131151251116.jpg)
python miniWeb框架搭建过程详解
2023-10-04 15:04:25
![](https://img.aspxhome.com/file/2023/4/68924_0s.png)
Python configparser模块应用过程解析
2022-08-03 19:56:11
PHP hex2bin()函数用法讲解
2023-06-06 18:51:35
![](https://img.aspxhome.com/file/2023/5/55385_0s.png)
python隐藏终端执行cmd命令的方法
2022-01-09 05:19:37
Python实现动态添加类的属性或成员函数的解决方法
2021-05-13 17:13:41
SQL“多字段模糊匹配关键字查询”
2008-04-24 14:16:00