Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

8. URL DISPATCH

8.2.4 Route Configuration Arguments

Route configuration add_route statements may specify a large number of arguments. They are documented as part of the API documentation at pyramid.config.Configurator.add_route().

Many of these arguments are route predicate arguments. A route predicate argument specifies that some aspect of the request must be true for the associated route to be considered a match during the route matching process. Examples of route predicate arguments are pattern, xhr, and request_method.

Other arguments are name and factory. These arguments represent neither predicates nor view configuration information.

latex-warning.png

Some arguments are view-configuration related arguments, such as view_renderer. These only have an effect when the route configuration names a view and these arguments have been deprecated as of Pyramid 1.1.

8.3 Route Matching

The main purpose of route configuration is to match (or not match) the PATH_INFO present in the WSGI environment provided during a request against a URL path pattern. PATH_INFO represents the path portion of the URL that was requested.

The way that Pyramid does this is very simple. When a request enters the system, for each route configuration declaration present in the system, Pyramid checks the request’s PATH_INFO against the pattern declared. This checking happens in the order that the routes were declared via pyramid.config.Configurator.add_route().

When a route configuration is declared, it may contain route predicate arguments. All route predicates associated with a route declaration must be True for the route configuration to be used for a given request during a check. If any predicate in the set of route predicate arguments provided to a route configuration returns False during a check, that route is skipped and route matching continues through the ordered set of routes.

78

8.3. ROUTE MATCHING

If any route matches, the route matching process stops and the view lookup subsystem takes over to find the most reasonable view callable for the matched route. Most often, there’s only one view that will match (a view configured with a route_name argument matching the matched route). To gain a better understanding of how routes and views are associated in a real application, you can use the pviews command, as documented in Displaying Matching Views for a Given URL.

If no route matches after all route patterns are exhausted, Pyramid falls back to traversal to do resource location and view lookup.

8.3.1 The Matchdict

When the URL pattern associated with a particular route configuration is matched by a request, a dictionary named matchdict is added as an attribute of the request object. Thus, request.matchdict will contain the values that match replacement patterns in the pattern element. The keys in a matchdict will be strings. The values will be Unicode objects.

latex-note.png

If no route URL pattern matches, the matchdict object attached to the request

will be None.

8.3.2 The Matched Route

When the URL pattern associated with a particular route configuration is matched by a request, an object named matched_route is added as an attribute of the request object. Thus, request.matched_route will be an object implementing the IRoute interface which matched the request. The most useful attribute of the route object is name, which is the name of the route that matched.

latex-note.png

If no route URL pattern matches, the matched_route object attached to the request will be None.

79

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]