【问题标题】:Python Server with CherryPy and Mako Template - Nothing is displayed in the browser带有 CherryPy 和 Mako 模板的 Python 服务器 - 浏览器中不显示任何内容
【发布时间】:2016-02-23 15:48:27
【问题描述】:

我有这个项目。

目录:

myProject
└── app
    ├── __init__.py
    ├── application.py 
    └── view.py
├── templates
    └── template1.mako
├── server.conf
└── server.py

server.py:

# coding: utf-8

import os.path
import cherrypy
import sys

from app import application

def main():

    try:
        currentDir = os.path.dirname(os.path.abspath(__file__))
    except:
        currentDir = os.path.dirname(os.path.abspath(sys.executable))
    cherrypy.Application.currentDir = currentDir

    configFileName = 'server.conf'
    if os.path.exists(configFileName) == False:
        configFileName = None

    cherrypy.engine.autoreload.unsubscribe()
    cherrypy.engine.timeout_monitor.unsubscribe()

    cherrypy.quickstart(application.Application(), config=configFileName)

if __name__ == '__main__':
    main()

server.conf:

[global]
tools.log_headers.on: True
tools.sessions.on:    False
tools.encode.on:      True
tools.encode.encoding:"utf-8"

server.socket_port:   8080
server.socket_timeout:60

server.thread_pool:  10
server.environment:  "production"
log.screen:          True

[/]
tools.staticdir.root: cherrypy.Application.currentDir
tools.staticdir.on = True
tools.staticdir.dir = '.'

template1.mako:

## coding: utf-8
<!DOCTYPE html>
<html>
    <head>
        <title>
            Forum
        </title>
        <meta charset="UTF-8" />
    </head>
    <body>
        <h1>
            Forum
        </h1>
        <form>
            Username: <input type="text" name="username"/>
            Password: <input type="password" name="password"/>
            <br/>
            <input type="submit" value="Submit!!!">
        </form>
        <div>
            Discussions
        </div>
    </body>
</html>
## EOF

应用程序.py:

import cherrypy
from app.view import View

class Application(object):

    def __init__(self):
        self.myView = View(cherrypy.Application.currentDir)

    def index(self):
        self.myView.create("template1.mako")
    index.exposed = True

view.py:

import os.path
from mako.lookup import TemplateLookup

class View(object):
    def __init__(self, path):
        self.templatesPath = os.path.join(path, 'templates')
        self.myLookup = TemplateLookup(directories=[self.templatesPath])

    def create(self, templateName):
        myTemplate = self.myLookup.get_template(templateName)
        return myTemplate.render()

当我在 myProject 文件夹中使用此命令在 cmd.exe 中启动服务器时:

python server.py

服务器工作:

ENGINE Listening for SIGTERM.
ENGINE Bus Starting
ENGINE Serving on http://127.0.0.1:8080
ENGINE Bus Started

但是当我在浏览器中访问 localhost:8080 时,什么都没有显示。只是白色。虽然没有错误。 我很确定server.py, server.conf and forum.mako 是正确的。 它与application.py and view.py 文件和导入有关。 我在 application.py 中导入 view.py:

from app.view import View

然后我使用 application.py 中的 View 类。我没有错误。 我不明白我的项目有什么问题。我从 5 个小时就坐在这里,试着理解一下。

【问题讨论】:

    标签: python python-3.x cherrypy mako


    【解决方案1】:

    application.py 中,您忘记返回渲染的模板。

    import cherrypy
    from app.view import View
    
    class Application(object):
    
        def __init__(self):
            self.myView = View(cherrypy.Application.currentDir)
    
        def index(self):
            return self.myView.create("template1.mako")
        index.exposed = True
    

    索引方法上的return

    【讨论】:

    • 非常感谢!现在可以了。 5 小时的故障排除,因为一点“回报”......
    猜你喜欢
    • 1970-01-01
    • 2017-02-07
    • 2014-03-13
    • 2014-10-29
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    相关资源
    最近更新 更多