Issue #1219 resolved

sessions in RoutesDispatcher

Sviatoslav Sydorenko
created an issue

Hi, is there any way of using cherrypy.session with RoutesDispatcher?

Comments (5)

  1. lakin.wecker
    import cherrypy                                                                                                                         
    
    class City:                                                                                                                             
        def __init__(self, name):                                                                                                           
            self.name = name                                                                                                                
            self.population = 10000                                                                                                         
    
        def index(self, **kwargs):                                                                                                          
            return "Welcome to %s, pop. %s - %s" % (self.name, self.population, cherrypy.session.get('your-name'))                          
        index._cp_config = {'tools.response_headers.on': True,                                                                              
                            'tools.response_headers.headers': [('Content-Language', 'en-GB')]}                                              
    
    
    def set_name(name):                                                                                                                     
        cherrypy.session['your-name'] = name                                                                                                
        return "<a href=\"/hounslow\">Hounslow</a>"                                                                                         
    
    
    
    d = cherrypy.dispatch.RoutesDispatcher()                                                                                                
    d.connect(action='index', name='hounslow', route='/hounslow',                                                                           
              controller=City('Hounslow'))                                                                                                  
    d.connect('set_name', route='/set-name/:name',                                                                                          
              controller=set_name)                                                                                                          
    
    
    conf = {                                                                                                                                
        '/': {                                                                                                                              
            'request.dispatch': d,                                                                                                          
            'tools.sessions.on': True,                                                                                                      
            'tools.sessions.storage_type': "ram",                                                                                           
            'tools.sessions.timeout': 60,                                                                                                   
        },                                                                                                                                  
    }                                                                                                                                       
    cherrypy.tree.mount(root=None, config=conf)                                                                                             
    cherrypy.engine.start()                                                                                                                 
    cherrypy.engine.block()
    
  2. Log in to comment