![](/img/trans.png)
[英]Python Server with CherryPy and Mako Template - Nothing is displayed in the browser
[英]Blank page through Mako template rendering in python / cherrypy
看来我终于可以让Mako
上班了。 至少在控制台中完成的所有工作均正常。 现在,我尝试用Mako
渲染index.html
,而我得到的只是一个空白页。 这是我称为的模块:
def index(self):
mytemplate = Template(
filename='index.html'
)
return mytemplate.render()
HTML是这样的:
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta charset="UTF-8" />
</head>
<body>
<p>This is a test!</p>
<p>Hello, my age is ${30 - 2}.</p>
</body>
</html>
因此,当我调用192.168.0.1:8081/index
(这是我运行的本地服务器设置)时,它将启动该功能,但浏览器中的结果是空白页。
我对Mako
理解正确还是错过了什么?
在基本用法中,所有内容都非常简单,并且有据可查 。 只要提供正确的引擎路径即可。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cherrypy
from mako.lookup import TemplateLookup
from mako.template import Template
path = os.path.abspath(os.path.dirname(__file__))
config = {
'global' : {
'server.socket_host' : '127.0.0.1',
'server.socket_port' : 8080,
'server.thread_pool' : 8
}
}
lookup = TemplateLookup(directories=[os.path.join(path, 'view')])
class App:
@cherrypy.expose
def index(self):
template = lookup.get_template('index.html')
return template.render(foo = 'bar')
@cherrypy.expose
def directly(self):
template = Template(filename = os.path.join(path, 'view', 'index.html'))
return template.render(foo = 'bar')
if __name__ == '__main__':
cherrypy.quickstart(App(), '/', config)
沿着Python文件,创建view
目录,并将以下内容放在index.html
下。
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta charset="UTF-8" />
</head>
<body>
<p>This is a ${foo} test!</p>
<p>Hello, my age is ${30 - 2}.</p>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.