[Webpy]在webpy中使用jinja2模板

webpy的模板感觉写小的网页还可以,但是如果要是写比较多的html标签就会显得非常乱,于是决定使用jinja2,这个模板类似django的模板,而且跟其他pythonweb框架的兼容性也比较好。

在项目目录下新建一个settings文件

#-*- coding: utf-8 -*-
__author__  = 'orangleliu'

'''
settings of the project
'''

import os
import web
from web.contrib.template import render_jinja

#------------------debug----------------------
debug = 'SERVER_SOFTWARE' not in os.environ
web.config.debug = debug


#------------------jinja2----------------------
app_root = os.path.dirname(__file__)
templates_path = os.path.join(app_root, 'templates').replace('\\', '/')
render = render_jinja(
    templates_path,
    encoding='utf-8'
)

要在项目目录下新建一个templates文件夹来存放模板文件。


使用:

#-*- coding: utf-8 -*-
__author__ = 'orangleliu'
'''
filename: index.py
create: @20140513
index page handler of this app
'''

import web
from settings import render

class index:
    def GET(self):
        web.header("Content-Type", "text/html")
        return render.index({})


项目文件的结构如下:



这样就可以使用jinja2模板了。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值