Does the complete dispatching process. `view_func` is called with the endpoint and a dict with the values for the view. It should look up the view function, call it, and return a response object or WSGI application. http exceptions are not caught by default so that
(
self,
view_func: t.Callable[[str, t.Mapping[str, t.Any]], WSGIApplication],
path_info: str | None = None,
method: str | None = None,
catch_http_exceptions: bool = False,
)
| 410 | self.websocket = self.url_scheme in {"ws", "wss"} |
| 411 | |
| 412 | def dispatch( |
| 413 | self, |
| 414 | view_func: t.Callable[[str, t.Mapping[str, t.Any]], WSGIApplication], |
| 415 | path_info: str | None = None, |
| 416 | method: str | None = None, |
| 417 | catch_http_exceptions: bool = False, |
| 418 | ) -> WSGIApplication: |
| 419 | """Does the complete dispatching process. `view_func` is called with |
| 420 | the endpoint and a dict with the values for the view. It should |
| 421 | look up the view function, call it, and return a response object |
| 422 | or WSGI application. http exceptions are not caught by default |
| 423 | so that applications can display nicer error messages by just |
| 424 | catching them by hand. If you want to stick with the default |
| 425 | error messages you can pass it ``catch_http_exceptions=True`` and |
| 426 | it will catch the http exceptions. |
| 427 | |
| 428 | Here a small example for the dispatch usage:: |
| 429 | |
| 430 | from werkzeug.wrappers import Request, Response |
| 431 | from werkzeug.wsgi import responder |
| 432 | from werkzeug.routing import Map, Rule |
| 433 | |
| 434 | def on_index(request): |
| 435 | return Response('Hello from the index') |
| 436 | |
| 437 | url_map = Map([Rule('/', endpoint='index')]) |
| 438 | views = {'index': on_index} |
| 439 | |
| 440 | @responder |
| 441 | def application(environ, start_response): |
| 442 | request = Request(environ) |
| 443 | urls = url_map.bind_to_environ(environ) |
| 444 | return urls.dispatch(lambda e, v: views[e](request, **v), |
| 445 | catch_http_exceptions=True) |
| 446 | |
| 447 | Keep in mind that this method might return exception objects, too, so |
| 448 | use :class:`Response.force_type` to get a response object. |
| 449 | |
| 450 | :param view_func: a function that is called with the endpoint as |
| 451 | first argument and the value dict as second. Has |
| 452 | to dispatch to the actual view function with this |
| 453 | information. (see above) |
| 454 | :param path_info: the path info to use for matching. Overrides the |
| 455 | path info specified on binding. |
| 456 | :param method: the HTTP method used for matching. Overrides the |
| 457 | method specified on binding. |
| 458 | :param catch_http_exceptions: set to `True` to catch any of the |
| 459 | werkzeug :class:`HTTPException`\\s. |
| 460 | """ |
| 461 | try: |
| 462 | try: |
| 463 | endpoint, args = self.match(path_info, method) |
| 464 | except RequestRedirect as e: |
| 465 | return e |
| 466 | return view_func(endpoint, args) |
| 467 | except HTTPException as e: |
| 468 | if catch_http_exceptions: |
| 469 | return e |