My web site works with a cherrypy server, and HTML/JS client. I want that that my website will have a static link, so no matter which page will be visited, the URL on the browser will be stay the same.
Currently the link is:
/static/index.html
I want that the link will be:
/home
Here is the website configuration:
class Root(object):
def __init__(self, target):
self.target_server = target
cherrypy.quickstart(webapp, '/', conf)
I have tried to change cherrypy.quickstart(webapp, '/', conf) to cherrypy.quickstart(webapp, '/home', conf), but it's just changed the link to /home/static/index.html.
Any idea?
import random
import string
import cherrypy
class MyHome(object):
@cherrypy.expose
def index(self):
raise cherrypy.HTTPRedirect("/home")
@cherrypy.expose
def home(self):
return "hello world"
if __name__ == '__main__':
cherrypy.quickstart(MyHome())
.......
dir_current = os.path.dirname(os.path.abspath(__file__))
......
class StaticFiles(object):
"""only there to have a class..."""
pass
# _cp_config = {
# 'tools.staticdir.on': True,
# # 'tools.staticdir.root': dir_static,
# # 'tools.staticdir.dir': '',
# 'tools.staticdir.index' : 'index.html'
# }
#
....
# config static tool
dir_static = os.path.join(dir_current, 'static')
configStatic = {
'/': {
'tools.staticdir.on': True,
'tools.staticdir.root': dir_static,
'tools.staticdir.dir': '',
'tools.staticdir.index': 'index.html'
},
}
cherrypy.tree.mount(StaticFiles(), '', configStatic)
....
# start cherrypy
print('')
print('******************************************')
print('')
print('cherrypy server - start engine:')
cherrypy.engine.start()
cherrypy.engine.block()[/static]
tools.staticdir.on = True
tools.staticdir.dir = "/home/site/static"
tools.staticdir.index = "index.html"
cherrypy.config.update({
'server.socket_port': 9090,
'server.socket_host': '0.0.0.0'
})
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd())
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './static/html'
'tools.staticdir.index': 'index.html'
},
'/js': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './static/js'
},
'/css': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './static/css'
},
'/img': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './static/img'
},
'/fonts': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './static/fonts'
}
}
class Root(object):
def __init__(self, target):
self.target_server = target
@cherrypy.expose
def index(self):
raise cherrypy.HTTPRedirect("/static/index.html")
cherrypy.quickstart(webapp, '/home', conf)But it didn't work.
import osimport cherrypyclass Root(object):passif __name__ == '__main__':root = os.path.dirname(os.path.abspath(__file__)) # this file's directoryprint rootcherrypy.config.update( {'server.socket_host': '0.0.0.0','server.socket_port': 8080,} )conf = {'/': {'tools.staticdir.on': True,'tools.staticdir.dir': root + '/public','tools.staticdir.index': '/public/views/index.html'}}cherrypy.quickstart(Root(), '/', conf)