regex URL mapping

606 views
Skip to first unread message

Tomas Brabenec

unread,
Dec 8, 2009, 5:57:31 AM12/8/09
to cherrypy-users
Hi all,

how can I use regular expression for url mapping?
In CP wiki is this example:

## example begin

def root_index(name):
return "Hello, %s!" % name

def branch_leaf(size):
return str(int(size) + 3)

mappings = [
(r'^/([^/]+)$', root_index),
(r'^/branch/leaf/(\d+)$', branch_leaf),
]

## example end


If I try add this to my code, its not working:


## code begin

import cherrypy

from context import Index
from context import Page2
from context import Page3

## Old mapping
#root = Index.Index()
#root.pg2 = Page2.Page2()
#root.pg2.edit = Page2.Edit()
#root.pg3 = Page3.Page3()
#cherrypy.tree.mount(root)

## New mapping
mappings = [
(r'^/([^/]+)$', Index.Index()),
(r'^/pg2(\d+)-(.*)/$', Page2.Page2()),
(r'^/pg2-edit(\d+)-(.*)/$', Page2.Edit()),
(r'^/pg3/$', Page3.Page3())
]
cherrypy.tree.mount(mappings)

if __name__ == '__main__':
import os.path
thisdir = os.path.dirname(__file__)
cherrypy.quickstart(config=os.path.join(thisdir, 'tutorial.conf'))

## code end


Where is a problem?

Tomas

Tomas Brabenec

unread,
Dec 9, 2009, 2:33:04 AM12/9/09
to cherryp...@googlegroups.com
Now, I try use Routes, but in this not working regular expression :-(

## code begin

import cherrypy

from context import Index
from context import Page2
from context import Page3

d = cherrypy.dispatch.RoutesDispatcher()

m = d.mapper
m.explicit = True
m.minimization = False
d.connect('index', '/', controller=Index.Index(), action='index')
d.connect('pg2', r'/pg2-(\d+)-(.*)/$', controller=Page2.Page2(),
action='index') # url example: http://myserver/pg2-1234-astalavista/
d.connect('pg2_edit', r'/pg2-edit-(\d+)-(.*)/$',
controller=Page2.Edit(), action='index') # url example:
http://myserver/pg2-edit-54-astalavista/
d.connect('pg3', '/pg3/$', controller=Page3.Edit(), action='index')

conf = {'/' : {'request.dispatch' : d}}
cherrypy.tree.mount(root=None, config=conf)

cherrypy.engine.start()

## code end

URLs "/" and "/pg3/" works correctly, but URLs with regular expression
not working (I get 404 Not Found), why?

Any idea ?


Tomas



Dne 8.12.2009 11:57, Tomas Brabenec napsal(a):

Tomas Brabenec

unread,
Dec 9, 2009, 3:23:56 AM12/9/09
to cherryp...@googlegroups.com
Here is resolution for my problem:

d.connect('pg2', '/pg2-{oid:(\d+)}-{name:(\w+)}/',
controller=Page2.Page2(), action='index')
d.connect('pg2_edit', '/pg2-edit-{oid:(\d+)}-{name:(\w+)}/',
controller=Page2.Edit2(), action='index')

or

d.connect('pg2', '/pg2-{oid}-{name}', controller=Page2.Page2(),
action='index', requirements=dict(oid=r"\d+", name=r"\w+"))
d.connect('pg2_edit', '/pg2-edit-{oid}-{name}/',
controller=Page2.Edit2(), action='index', requirements=dict(oid=r"\d+",
name=r"\w+"))


Tomas


Dne 9.12.2009 8:33, Tomas Brabenec napsal(a):
> --
>
> You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
> To post to this group, send email to cherryp...@googlegroups.com.
> To unsubscribe from this group, send email to cherrypy-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en.
>
>
>

Sylvain Hellegouarch

unread,
Dec 9, 2009, 3:06:53 AM12/9/09
to cherryp...@googlegroups.com

> Now, I try use Routes, but in this not working regular expression :-(
>

If you need regex you may want to look at selector:

http://trac.defuze.org/browser/oss/cpsel
http://lukearno.com/projects/selector/

- Sylvain
--
Sylvain Hellegouarch
http://www.defuze.org
Reply all
Reply to author
Forward
0 new messages