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
Provenance
- Commit
df1dd57- Tree
7309dac- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
32cdb72
2 files changed+114 added−59 removed
modified:docs/api.rst+97 added
476 unmodified lines477 478 .. autoclass:: flask.views.MethodView479 :members:480 481+.. _url-route-registrations:482+483+URL Route Registrations484+-----------------------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 system491+ which is exposed as :attr:`flask.Flask.url_map`.492+493+Variables parts in the route can be specified with angular brackets494+(``/user/<username>``). By default a variable part in the URL accepts any495+string without a slash however a different converter can be specified as496+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 integers505+`float` like `int` but for floating point values506+`path` like the default but also accepts slashes507+=========== ===============================================508+509+Here some examples::510+511+ @app.route('/')512+ def index():513+ pass514+515+ @app.route('/<username>')516+ def show_user(username):517+ pass518+519+ @app.route('/post/<int:post_id>')520+ def show_post(post_id):521+ pass522+523+An important detail to keep in mind is how Flask deals with trailing524+slashes. The idea is to keep each URL unique so the following rules525+apply:526+527+1. 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.530+2. 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.532+533+This is consistent with how web servers deal with static files. This534+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 be537+unique however. Defaults can also be specified. Here for example is a538+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+ pass544+545+This specifies that ``/users/`` will be the URL for page one and546+``/users/page/N`` will be the URL for page `N`.547+548+Here the parameters that :meth:`~flask.Flask.route` and549+:meth:`~flask.Flask.add_url_rule` accept. The only difference is that550+with the route parameter the view function is defined with the decorator551+instead of the `view_func` parameter.552+553+=============== ==========================================================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+=============== ==========================================================577+578 .. _view-func-options:579 580 View Function Options581 ---------------------36 unmodified linesmodified:flask/app.py+17 added-59 removed
822 unmodified lines823 to a view function like so::824 825 app.view_functions['index'] = index826 827- 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.830 831+ For more information refer to :ref:`url-route-registrations`.832+833 .. versionchanged:: 0.2834 `view_func` parameter added.835 836 .. versionchanged:: 0.649 unmodified lines886 self.view_functions[endpoint] = view_func887 888 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::892 893 @app.route('/')894 def index():895 return 'Hello World'896 894- 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`.898 899- Variable parts are passed to the view function as keyword900- arguments.901-902- The following converters are possible:903-904- =========== ===========================================905- `int` accepts integers906- `float` like `int` but for floating point values907- `path` like the default but also accepts slashes908- =========== ===========================================909-910- Here some examples::911-912- @app.route('/')913- def index():914- pass915-916- @app.route('/<username>')917- def show_user(username):918- pass919-920- @app.route('/post/<int:post_id>')921- def show_post(post_id):922- pass923-924- 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:927-928- 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.933-934- This is consistent with how web servers deal with static files. This935- also makes it possible to use relative link targets safely.936-937- The :meth:`route` decorator accepts a couple of other arguments938- as well:939-899 :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