Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 52 lines (33 sloc) 2.787 kb
680ad55 @xrfang edit
xrfang authored
1 ---
2 layout: default
3 title: Understanding URL Handling
4 ---
5
6 # Understanding URL Handling
7
24ffdba Add link to french translation
nux authored
8 Other languages : [français](/../cookbook/url_handling/fr) | ...
9
a875f8c @xrfang Initial version
xrfang authored
10 `Problem`: how to design a url handling / dispatching scheme for the entire site
11
12 `Solution`:
13
14 web.py's URL handling scheme is simple yet powerful and flexible. at the top of each application, you usually see the full URL dispatching scheme defined as a tuple:
15
16 urls = (
17 "/tasks/?", "signin",
18 "/tasks/list", "listing",
19 "/tasks/post", "post",
20 "/tasks/chgpass", "chgpass",
21 "/tasks/act", "actions",
22 "/tasks/logout", "logout",
23 "/tasks/signup", "signup"
24 )
25
615308f @rmarchildon Fix incorrect url
rmarchildon authored
26 The format of this tuple is: _url-path-pattern_, _handler-class_ this pattern will repeat as more url patterns are defined. If you don't understand the relationship between url pattern and handler classes, please read the [Hello World example](/helloworld) or [Quick Start Tutorial](/tutorial3.en) before reading any other cookbook recipes.
a875f8c @xrfang Initial version
xrfang authored
27
28 `Path Matching`
29
30 You can utilize the power of regular expressions to design more flexible url patterns. For example, /(test1|test2) will catch either /test1 or /test2. The key point to understand is that this matching happens on the `path` of your URL. For example, the following URL:
31
32 http://localhost/myapp/greetings/hello?name=Joe
33
34 The path of this URL is _/myapp/greetings/hello_. web.py will internally add ^ and $ to the url pattern so that the pattern _/tasks/_ will not match _/tasks/addnew_. As it matches against the path, you can not use a pattern like: _/tasks/delete?name=(.+)_ as the part after ? is called `query` and is not matched against. For a detailed description of URL components, please read [web.ctx](/cookbook/ctx).
35
36 `Capture Parameters`
37
38 In the url pattern you can catch parameters which can be used in your handler class:
39
40 /users/list/(.+), "list_users"
41
42 The chunk after _list/_ are captured and can be used as a parameter in GET or POST:
43
44 class list_users:
45 def GET(self, name):
46 return "Listing info about user: {0}".format(name)
47
48 You can define more than one parameters as you wish. Also note that URL query parameters (which appears after the ?) can be obtained using [web.input()](/cookbook/input)
49
50 `Note on sub-applications`
51
52 To better handle larger web applications, web.py support [sub-applications](/cookbook/subapp). While designing url scheme with sub applications, keep in mind that the path (web.ctx.path) will get the parent path stripped off. e.g. if in the main application, you define to forward url pattern "/blog" to the 'blog' sub-application, in your blog sub-application all url patterns starts with "/", `not` /blog. Read the [web.ctx](/cookbook/ctx) cookbook recipe for more details.
Something went wrong with that request. Please try again.