Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python/cherrypy中通过Mako模板呈现空白页面_Python_Templates_Cherrypy_Mako - Fatal编程技术网

在python/cherrypy中通过Mako模板呈现空白页面

在python/cherrypy中通过Mako模板呈现空白页面,python,templates,cherrypy,mako,Python,Templates,Cherrypy,Mako,看来我终于可以让Mako工作了。 至少在控制台中所做的每件事都能正常工作。 现在我试图用Mako呈现我的index.html,我得到的只是一个空白页面。 这是我调用的模块: def index(self): mytemplate = Template( filename='index.html' ) return mytemplate.render() html是这样的: <!DOCTY

看来我终于可以让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>

标题
这是一个考验

你好,我的年龄是${30-2}

因此,当我调用
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>

标题
这是一个${foo}测试

你好,我的年龄是${30-2}


这太棒了。我不太清楚我和Serv.Py的区别是什么(稍后我会查查看),但是我会用你的作为基础,并最终开始处理真正的事情。谢谢你!您能否解释一下,在您的示例中,index和directive模块之间的区别是什么?这两种方法似乎都是一样的,但我认为在启动服务器时只使用了“index”模块,不?
TemplateLookup
是一种文件系统查找。告诉它一次模板根目录的位置,然后让它获取一个模板,比如
lookup.get\u template('user/cabinet.html')
。您可以自己做同样的事情,比如在
方法中直接指定完整的模板文件名。我给了你相关文档页面的链接。花点精力去理解你选择的模板引擎是如何工作的。好吧,我已经阅读了文档。无论如何,这是我自己努力让它工作的必要条件。我只是想澄清一下,在你把这两个模块放进去的背后,是否有更深层次的含义,因为它们的作用是一样的。无论如何谢谢~没问题。对我来说,已经足够清楚了,所以我想确保你读过它。关于“更深的意义”,当你可以手工完成所有的日常工作时,你可以直接使用
模板
。当您将其自动化时,
TemplateLookup
将起到解救作用(就像您之前关于完整生成解决方案的问题一样)。