What are some guidelines for structuring cherry applications?

I am writing a cherry application and I was wondering what is the best way to structure my handlers and code for larger applications?

I understand that assignment is simple through cherrypy.root, but what are some methods of writing handlers and their purpose?

(Let me prove my confusion!) My initial thought was to write a standard handler class that runs the template to run based on the current URL combination or class / method. Then I would assign one instance of this handler several times to the page creation path. I do not see this working, as recursive links will not work correctly.

So, given the fact that I am already drawing spaces for what my own source code should look like, I would like some pointers and examples!

Feel free to ask me any detailed questions. Despite the fact that there is a lot of material for the vortex textbook, it tends to only scratch the surface.

+5
source share
2 answers

This question is wildly subjective, but I will give it a chance.

  • First of all, always keep the database and data code separate from the web code. I have many small files with one class in a folder DB/, all of which are combined into a file Base.py, for example:

    Web /
      Base.py - The main "base" class that includes classes in others
       ,  -  __init__
      Users.py - ,   ,  ,  "DB/Users.py" 
          ..   ( 
           DB)
     ...
    DB/
      Base.py -    DB,      DB. 
       SQLAlchemy/      , 
          ..     ,    
      ,     ..   ,  
            
      Users.py -     /  .. DB
     ...
     /
      (HTML-  )
     /
      (  /CSS/javscript   ..)
    
    , __init__.py , , python
  • , , , . , , , , a foolish consistency is the hobgoblin of small minds, python: -)

  • , . , - . , ,

  • , , - Class . Base.py:
    import Users
    class Base(Users.Class):
        def __init__(self):
            Users.Class.__init__(self)
    
    , , from Users import Users , Users.py from Base import x, . , , : -P

, .

+5

CherryPy , , , , . , , ; , __init__ .

. - , ; URI URI , . , , ( ) ( ). , .

, CherryPy , ​​ , . 1) , URI, URI, (. http://docs.cherrypy.org/dev/intro/concepts/config.html ) 2) , URI, URI, , . . http://docs.cherrypy.org/dev/intro/concepts/tools.html

, : cherrypy.root :

def make_app():
    root = Root()
    root.foo = Foo()
    root.bars = BarCollection()
    return root

Root, Foo Bar . , , "infer templates". :

from cherrypy import expose

class Foo(MyAppBase):
    @expose()
    def index(self, a, b, c):
        ...

root.foo = Foo(template='foo.html')

:

from cherrypy import expose, tools

class Foo(object):
    @tools.render(template='foo.html')
    @expose()
    def index(self, a, b, c):
        ...

root.foo = Foo()

... "tools.render" - CherryPy, , . :

[/foo/]
tools.render.template = 'foo2.html'
+10

All Articles