Mako模板渲染中的空白页面在python/cherrypy中

2024-05-19 09:34:38 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我终于可以让Mako工作了。 至少在控制台中所做的一切都正常。 现在我试着用Mako呈现我的index.html,得到的只是一个空白页。 这是我称之为的模块:

    def index(self):
    mytemplate = Template(
                    filename='index.html'
                )   
    return mytemplate.render()

html是这样的:

^{pr2}$

因此,当我调用192.168.0.1:8081/index(这是我运行的本地服务器设置)时,它会启动函数,但我的浏览器中的结果是一个空白页。在

我正确地理解了Mako还是我遗漏了什么?在


Tags: 模块函数self服务器indexreturnmakodef
1条回答
网友
1楼 · 发布于 2024-05-19 09:34:38

在基本用法中,一切都很简单,well documented。只需提供正确的引擎路径。在

#!/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下。在

^{pr2}$

相关问题 更多 >

    热门问题