I have this project.
Directory:
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
application.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()
When I start the server in cmd.exe with this command within the myProject folder:
python server.py
The server works:
ENGINE Listening for SIGTERM.
ENGINE Bus Starting
ENGINE Serving on http://127.0.0.1:8080
ENGINE Bus Started
But when I go to localhost:8080 in the Browser, nothing is displayed. Just white. Although there are no errors.
I am pretty sure that server.py, server.conf and forum.mako
are correct.
It has something to do with the application.py and view.py
files and the import.
I import view.py in application.py with:
from app.view import View
And then I use the View class in application.py. I get no errors. I don't understand what's wrong with my Project. I am sitting here since 5 hours and try do understand.
In application.py
you forgot to return the rendered template.
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
The return
on the index method.