Skip to content
Myr

Cleaned up url routing common docs. This fixes #279

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 32cdb72 · Browse files · 2 files+114 added−59 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+114 added−59 removed

  • other:+114 added−59 removed

2 files+114 added−59 removed

modified:docs/api.rst+97 added
476 unmodified lines
477478.. autoclass:: flask.views.MethodView479 :members:480481.. _url-route-registrations:482483URL Route Registrations484-----------------------485486Generally there are three ways to define rules for the routing system:4874881. You can use the :meth:`flask.Flask.route` decorator.4892. You can use the :meth:`flask.Flask.add_url_rule` function.4903. You can directly access the underlying Werkzeug routing system491 which is exposed as :attr:`flask.Flask.url_map`.492493Variables parts in the route can be specified with angular brackets494(``/user/<username>``). By default a variable part in the URL accepts any495string without a slash however a different converter can be specified as496well by using ``<converter:name>``.497498Variable parts are passed to the view function as keyword arguments.499500The following converters are possible available:501502=========== ===============================================503`unicode` accepts any text without a slash (the default)504`int` accepts integers505`float` like `int` but for floating point values506`path` like the default but also accepts slashes507=========== ===============================================508509Here some examples::510511 @app.route('/')512 def index():513 pass514515 @app.route('/<username>')516 def show_user(username):517 pass518519 @app.route('/post/<int:post_id>')520 def show_post(post_id):521 pass522523An important detail to keep in mind is how Flask deals with trailing524slashes. The idea is to keep each URL unique so the following rules525apply:5265271. If a rule ends with a slash and is requested without a slash by the528 user, the user is automatically redirected to the same page with a529 trailing slash attached.5302. If a rule does not end with a trailing slash and the user request the531 page with a trailing slash, a 404 not found is raised.532533This is consistent with how web servers deal with static files. This534also makes it possible to use relative link targets safely.535536You can also define multiple rules for the same function. They have to be537unique however. Defaults can also be specified. Here for example is a538definition for a URL that accepts an optional page::539540 @app.route('/users/', defaults={'page': 1})541 @app.route('/users/page/<int:page>')542 def show_users(page):543 pass544545This specifies that ``/users/`` will be the URL for page one and546``/users/page/N`` will be the URL for page `N`.547548Here the parameters that :meth:`~flask.Flask.route` and549:meth:`~flask.Flask.add_url_rule` accept. The only difference is that550with the route parameter the view function is defined with the decorator551instead of the `view_func` parameter.552553=============== ==========================================================554`rule` the URL roule as string555`endpoint` the endpoint for the registered URL rule. Flask itself556 assumes that the name of the view function is the name557 of the endpoint if not explicitly stated.558`view_func` the function to call when serving a request to the559 provided endpoint. If this is not provided one can560 specify the function later by storing it in the561 :attr:`~flask.Flask.view_functions` dictionary with the562 endpoint as key.563`defaults` A dictionary with defaults for this rule. See the564 example above for how defaults work.565`subdomain` specifies the rule for the subdomain in case subdomain566 matching is in use. If not specified the default567 subdomain is assumed.568`**options` the options to be forwarded to the underlying569 :class:`~werkzeug.routing.Rule` object. A change to570 Werkzeug is handling of method options. methods is a list571 of methods this rule should be limited to (`GET`, `POST`572 etc.). By default a rule just listens for `GET` (and573 implicitly `HEAD`). Starting with Flask 0.6, `OPTIONS` is574 implicitly added and handled by the standard request575 handling. They have to be specified as keyword arguments.576=============== ==========================================================577578.. _view-func-options:579580View Function Options581---------------------
36 unmodified lines
modified:flask/app.py+17 added-59 removed
822 unmodified lines
823 to a view function like so::824825 app.view_functions['index'] = index826827 If a view function is provided some defaults can be specified directly828 on the view function. For more information refer to829 :ref:`view-func-options`.827 Internally :meth:`route` invokes :meth:`add_url_rule` so if you want828 to customize the behavior via subclassing you only need to change829 this method.830831 For more information refer to :ref:`url-route-registrations`.832833 .. versionchanged:: 0.2834 `view_func` parameter added.835836 .. versionchanged:: 0.6
49 unmodified lines
886 self.view_functions[endpoint] = view_func887888 def route(self, rule, **options):889 """A decorator that is used to register a view function for a888 given URL rule. Example::890 given URL rule. This does the same thing as :meth:`add_url_rule`891 but is intended for decorator usage::892893 @app.route('/')894 def index():895 return 'Hello World'896894 Variables parts in the route can be specified with angular895 brackets (``/user/<username>``). By default a variable part896 in the URL accepts any string without a slash however a different897 converter can be specified as well by using ``<converter:name>``.897 For more information refer to :ref:`url-route-registrations`.898899 Variable parts are passed to the view function as keyword900 arguments.901902 The following converters are possible:903904 =========== ===========================================905 `int` accepts integers906 `float` like `int` but for floating point values907 `path` like the default but also accepts slashes908 =========== ===========================================909910 Here some examples::911912 @app.route('/')913 def index():914 pass915916 @app.route('/<username>')917 def show_user(username):918 pass919920 @app.route('/post/<int:post_id>')921 def show_post(post_id):922 pass923924 An important detail to keep in mind is how Flask deals with trailing925 slashes. The idea is to keep each URL unique so the following rules926 apply:927928 1. If a rule ends with a slash and is requested without a slash929 by the user, the user is automatically redirected to the same930 page with a trailing slash attached.931 2. If a rule does not end with a trailing slash and the user request932 the page with a trailing slash, a 404 not found is raised.933934 This is consistent with how web servers deal with static files. This935 also makes it possible to use relative link targets safely.936937 The :meth:`route` decorator accepts a couple of other arguments938 as well:939899 :param rule: the URL rule as string900 :param endpoint: the endpoint for the registered URL rule. Flask901 itself assumes the name of the view function as902 endpoint903 :param view_func: the function to call when serving a request to the904 provided endpoint905 :param options: the options to be forwarded to the underlying906 :class:`~werkzeug.routing.Rule` object. A change907 to Werkzeug is handling of method options. methods941 :param methods: a list of methods this rule should be limited908 is a list of methods this rule should be limited909 to (`GET`, `POST` etc.). By default a rule910 just listens for `GET` (and implicitly `HEAD`).911 Starting with Flask 0.6, `OPTIONS` is implicitly912 added and handled by the standard request handling.946 :param subdomain: specifies the rule for the subdomain in case947 subdomain matching is in use.948 :param strict_slashes: can be used to disable the strict slashes949 setting for this rule. See above.950 :param endpoint: Since version 0.8 you can also pass the enpoint,951 it will be used instead of generating the endpoint952 from the function name.953 :param options: other options to be forwarded to the underlying954 :class:`~werkzeug.routing.Rule` object.913 """914 def decorator(f):915 endpoint = options.pop('endpoint', None)916 self.add_url_rule(rule, endpoint, f, **options)
595 unmodified lines