2

I don't know how make routings in cherrypy and how define css and js file in conf in index.py

I have a structure:

index.py
|
-views
  |
   -home
   |
    -index.html
    -login.html
-public
 |
  -css
   bootstrap.css
   ...
  -js
  -images

And when I start Cherrypy server with index.html in my site I have 404 in css file and when I click button with href to login.html I can't go there, because I see 404. Please a help because I am newbie in `Cherrypy.

    import cherrypy
    import webbrowser
    import os, os.path
    VIEWS_DIR = os.path.join(os.path.abspath("."), u"VIEWS")
    CSS_DIR = os.path.join(os.path.abspath("."), u"CSS")

    class Main(object):
        @cherrypy.expose()
        def index(self):
            return open('views/home/index.html', 'rb').read().decode('utf-8')
    conf = {
                '/': {
                'tools.sessions.on': True,
                'tools.staticdir.root': os.path.abspath(os.getcwd()),
                },
                '/static/views/home':
                     {'tools.staticdir.on': True,
                      'tools.staticdir.dir': VIEWS_DIR,
                    },
                '/bootstrap.css':
                     { 'tools.staticfile.on':True,
                      'tools.staticfile.filename': os.path.abspath ("./public/css/bootstrap.css"),
                     },
            }

    def open_page():
        webbrowser.open("http://127.0.1.1:8080/")
    cherrypy.engine.subscribe('start', open_page)
    cherrypy.tree.mount(Main(), '/', conf)
    cherrypy.engine.start()

404 error in css file and when I click to href link in site index.html

Aksen P
  • 4,564
  • 3
  • 14
  • 27
Tom
  • 31
  • 3

2 Answers2

0

To work as you expected your hrefs should start either with /static/views/home or /static/public/css plus target file names. According to your structure you should map two static routes like in my example

conf = {
    '/': {
        'tools.sessions.on': True,
        'tools.staticdir.root': os.path.abspath(os.getcwd()),
    },
    '/static/views/home': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': './views/home',
    },
    '/static/public/css': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': './public/css',
    }
}

and subsequently your html should looks like

<!DOCTYPE html>
<html>
  <head>
    <link href="/static/public/css/style.css" rel="stylesheet">
  </head>
  <body>
    <p>
        <a href="/static/views/home/login.html">Login</a>
    </p>
  </body>
</html>
prodops
  • 11
  • 1
  • 3
0

@prodops I solved it in another way. In index.py:

class Main:
    @cherrypy.expose()
    def index(self):
        return open('views/home/index.html', 'rb').read().decode('utf-8')
    @cherrypy.expose()
    def secondsite(self):
        return open('views/home/secondsite.html', 'rb').read().decode('utf-8')

conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd()),
        },
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': './public',
        }
    }

and in index.html site I have

<a href="secondsite">
   <button class="btn btn-success" type="button">
      <p>BUTTON</p>
   </button>
</a>
Tom
  • 31
  • 3