Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:35:15+00:00 2026-06-17T16:35:15+00:00

I am new to CherryPy. I am using the default dispatcher, with a URL

  • 0

I am new to CherryPy. I am using the default dispatcher, with a URL structure similar to this:

root = Root()
root.page1 = Page1()
root.page1.apple = Apple()
root.page2 = Page2()
root.page2.orange = Orange()

Orange renders a template, in which I need a link to Apple. I could just hardcode /page1/apple/. But how can I get the URL of Apple in a DRY manner?

Can this be done with the default dispatcher in CherryPy, or is it only possible with the Routes dispatcher?

(I am coming from the Django world, where one would use reverse() for this purpose.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:35:16+00:00Added an answer on June 17, 2026 at 4:35 pm

    You can access to the mounted apps through

    cherrypy.tree.apps[mount_point].root
    

    root is always the mounted instance to the mount point. So a reverse function would look like:

    def reverse(cls):
        # get link to a class type
        for app_url in cherrypy.tree.apps.keys():
            if isinstance(cherrypy.tree.apps[app_url].root, cls):
                # NOTE: it will return with the first mount point of this class
                return app_url
    

    Please find a sample code below that uses your classes. http://localhost:8080/page4/orange/ prints out { Orange and the link to apple: : "/page3/apple" }

    import cherrypy
    
    link_to_apple_global = ''
    
    class Orange(object):
        def __init__(self):
            pass
        @cherrypy.expose
        @cherrypy.tools.json_out()
        def index(self):
            return {"Orange and the link to apple: ": link_to_apple_global}
    class Page2(object):
    
        def __init__(self):
            pass
        @cherrypy.expose
        def index(self):
            return "Page2"
    class Apple(object):
    
        def __init__(self):
            pass
        @cherrypy.expose
        def index(self):
            return "Apple"
    
    class Page1(object):
    
        def __init__(self):
            pass
        @cherrypy.expose
        def index(self):
            return "Page1"
    
    class Root(object):
    
        def __init__(self):
            pass
        @cherrypy.expose
        def index(self):
            return "Root"            
    
    def reverse(cls):
        #return cherrypy.tree.apps.keys()
        #return dir(cherrypy.tree.apps[''].root)
        #return dir(cherrypy.tree.apps['/page3/apple'].root)
        # get link to apple
        for app_url in cherrypy.tree.apps.keys():
            if isinstance(cherrypy.tree.apps[app_url].root, cls):
                # NOTE: it will return with the first instance
                return app_url
    
    root = Root()
    root.page1 = Page1()
    root.page1.apple = Apple()
    root.page2 = Page2()
    root.page2.orange = Orange()
    
    cherrypy.tree.mount(root, '/')
    # if you do not specify the mount points you can find the objects
    # in cherrypy.tree.apps[''].root...
    cherrypy.tree.mount(root.page1, '/page4')
    cherrypy.tree.mount(root.page2, '/page3')
    cherrypy.tree.mount(root.page2.orange, '/page4/orange')
    cherrypy.tree.mount(root.page1.apple, '/page3/apple')
    
    link_to_apple_global = reverse(Apple)
    
    cherrypy.engine.start()
    cherrypy.engine.block()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to cherrypy and am trying to mount a simple hello world
new on ruby and using windows xp and rails 3, i want to send
New to PHP and MySQL, have heard amazing things about this website from Leo
New to Regex. I want to validate to this format: Any character allowed, except
New programmer here, I am trying to understand and break down this code below
New developer here,Im using the Custom Image Picker by ray wenderlich. But what I
I implemented HTTP digest authentication in my CherryPy application using the built-in tools.auth_digest tool.
New to Android before I port my application to this platform I wanted to
** New EDIT ** so what I'm trying to do is this. I want
New to Javascript and jQuery, so I assume this is a pretty simple question.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.