@@ -478,6 +478,103 @@ Class Based Views
478
478
.. autoclass :: flask.views.MethodView
479
479
:members:
480
480
481
+ .. _url-route-registrations :
482
+
483
+ URL Route Registrations
484
+ -----------------------
485
+
486
+ Generally there are three ways to define rules for the routing system:
487
+
488
+ 1. You can use the :meth: `flask.Flask.route ` decorator.
489
+ 2. You can use the :meth: `flask.Flask.add_url_rule ` function.
490
+ 3. You can directly access the underlying Werkzeug routing system
491
+ which is exposed as :attr: `flask.Flask.url_map `.
492
+
493
+ Variables parts in the route can be specified with angular brackets
494
+ (``/user/<username> ``). By default a variable part in the URL accepts any
495
+ string without a slash however a different converter can be specified as
496
+ well by using ``<converter:name> ``.
497
+
498
+ Variable parts are passed to the view function as keyword arguments.
499
+
500
+ The following converters are possible available:
501
+
502
+ =========== ===============================================
503
+ `unicode ` accepts any text without a slash (the default)
504
+ `int ` accepts integers
505
+ `float ` like `int ` but for floating point values
506
+ `path ` like the default but also accepts slashes
507
+ =========== ===============================================
508
+
509
+ Here some examples::
510
+
511
+ @app.route('/')
512
+ def index():
513
+ pass
514
+
515
+ @app.route('/<username>')
516
+ def show_user(username):
517
+ pass
518
+
519
+ @app.route('/post/<int:post_id>')
520
+ def show_post(post_id):
521
+ pass
522
+
523
+ An important detail to keep in mind is how Flask deals with trailing
524
+ slashes. The idea is to keep each URL unique so the following rules
525
+ apply:
526
+
527
+ 1. If a rule ends with a slash and is requested without a slash by the
528
+ user, the user is automatically redirected to the same page with a
529
+ trailing slash attached.
530
+ 2. If a rule does not end with a trailing slash and the user request the
531
+ page with a trailing slash, a 404 not found is raised.
532
+
533
+ This is consistent with how web servers deal with static files. This
534
+ also makes it possible to use relative link targets safely.
535
+
536
+ You can also define multiple rules for the same function. They have to be
537
+ unique however. Defaults can also be specified. Here for example is a
538
+ definition for a URL that accepts an optional page::
539
+
540
+ @app.route('/users/', defaults={'page': 1})
541
+ @app.route('/users/page/<int:page>')
542
+ def show_users(page):
543
+ pass
544
+
545
+ This specifies that ``/users/ `` will be the URL for page one and
546
+ ``/users/page/N `` will be the URL for page `N `.
547
+
548
+ Here the parameters that :meth: `~flask.Flask.route ` and
549
+ :meth: `~flask.Flask.add_url_rule ` accept. The only difference is that
550
+ with the route parameter the view function is defined with the decorator
551
+ instead of the `view_func ` parameter.
552
+
553
+ =============== ==========================================================
554
+ `rule ` the URL roule as string
555
+ `endpoint ` the endpoint for the registered URL rule. Flask itself
556
+ assumes that the name of the view function is the name
557
+ of the endpoint if not explicitly stated.
558
+ `view_func ` the function to call when serving a request to the
559
+ provided endpoint. If this is not provided one can
560
+ specify the function later by storing it in the
561
+ :attr: `~flask.Flask.view_functions ` dictionary with the
562
+ endpoint as key.
563
+ `defaults ` A dictionary with defaults for this rule. See the
564
+ example above for how defaults work.
565
+ `subdomain ` specifies the rule for the subdomain in case subdomain
566
+ matching is in use. If not specified the default
567
+ subdomain is assumed.
568
+ `**options ` the options to be forwarded to the underlying
569
+ :class: `~werkzeug.routing.Rule ` object. A change to
570
+ Werkzeug is handling of method options. methods is a list
571
+ of methods this rule should be limited to (`GET `, `POST `
572
+ etc.). By default a rule just listens for `GET ` (and
573
+ implicitly `HEAD `). Starting with Flask 0.6, `OPTIONS ` is
574
+ implicitly added and handled by the standard request
575
+ handling. They have to be specified as keyword arguments.
576
+ =============== ==========================================================
577
+
481
578
.. _view-func-options :
482
579
483
580
View Function Options
0 commit comments