Skip to content
Myr

wrap lines

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

tighten up wording remove any converter from quickstart use correct rst code syntax

authored by David Lord · committed by David Lord · parent dfb03c5 · Browse files · 1 file+83 added−55 removed
This commit

State

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

Diff stats

1 file+83 added−55 removed

  • other:+83 added−55 removed

1 file+83 added−55 removed

modified:docs/quickstart.rst+83 added-55 removed
157 unmodified lines
158159Routing160-------161162Modern web applications use meaningful URLs to help users. Users are more likely to like a page and come back if the page uses a meaningful URL they can remember and use to directly visit a page. 162Modern web applications use meaningful URLs to help users. Users are more163likely to like a page and come back if the page uses a meaningful URL they can164remember and use to directly visit a page.165164As you saw in the minimal application, you can use the :meth:`~flask.Flask.route` decorator to bind a function to a meaningful URL. For example::166Use the :meth:`~flask.Flask.route` decorator to bind a function to a URL. ::167168 @app.route('/')169 def index():170 return 'Index Page'
1 unmodified line
172 @app.route('/hello')173 def hello():174 return 'Hello, World'175174You can do more! You can make parts of the URL dynamic and attach multiple rules to a function.176You can do more! You can make parts of the URL dynamic and attach multiple177rules to a function.178179Variable Rules180``````````````181179You can add variable sections to a URL by marking sections with ``<variable_name>``. Your function then receives the ``<variable_name>`` as a keyword argument. Optionally, you can use a converter to specify a rule with ``<converter:variable_name>``. For example::182You can add variable sections to a URL by marking sections with183``<variable_name>``. Your function then receives the ``<variable_name>``184as a keyword argument. Optionally, you can use a converter to specify the type185of the argument like ``<converter:variable_name>``. ::186187 @app.route('/user/<username>')188 def show_user_profile(username):189 # show the user profile for that user
10 unmodified lines
200 return 'Subpath %s' % subpath201202Converter types:203198=========== ===============================================199`string` (default) accepts any text without a slash200`int` accepts integers201`float` accepts floating point values202`path` like `string` but also accepts slashes203`any` matches one of the items provided204`uuid` accepts UUID strings205=========== ===============================================204========== ==========================================205``string`` (default) accepts any text without a slash206``int`` accepts positive integers207``float`` accepts positive floating point values208``path`` like ``string`` but also accepts slashes209``uuid`` accepts UUID strings210========== ==========================================211207.. admonition:: Unique URLs / Redirection Behavior212Unique URLs / Redirection Behavior213``````````````````````````````````214209 Flask's URL rules are based on Werkzeug's routing module. The routing module creates meaningful and unique URLs based on precedents laid down by Apache and earlier HTTP servers.215Take these two rules::216211 Take these two rules::217 @app.route('/projects/')218 def projects():219 return 'The project page'220213 @app.route('/projects/')214 def projects():215 return 'The project page'221 @app.route('/about')222 def about():223 return 'The about page'224217 @app.route('/about')218 def about():219 return 'The about page'225Though they look similar, they differ in their use of the trailing slash in226the URL. In the first case, the canonical URL for the ``projects`` endpoint227uses a trailing slash. It's similar to a folder in a file system; if you228access the URL without a trailing slash, Flask redirects you to the229canonical URL with the trailing slash.230221 Though they look similar, they differ in their use of the trailing slash in the URL *definition*. In the first case, the canonical URL for the ``projects`` endpoint uses a trailing slash. It's similar to a folder in a file system, and if you access the URL without a trailing slash, Flask redirects you to the canonical URL with the trailing slash.231In the second case, however, the URL definition lacks a trailing slash,232like the pathname of a file on UNIX-like systems. Accessing the URL with a233trailing slash produces a 404 “Not Found” error.234223 In the second case, however, the URL definition lacks a trailing slash like the pathname of a file on UNIX-like systems. Accessing the URL with a trailing slash produces a 404 “Not Found” error.235This behavior allows relative URLs to continue working even if the trailing236slash is omitted, consistent with how Apache and other servers work. Also,237the URLs will stay unique, which helps search engines avoid indexing the238same page twice.239225 This behavior allows relative URLs to continue working even if the trailing226 slash is omitted, consistent with how Apache and other servers work. Also,227 the URLs will stay unique, which helps search engines avoid indexing the228 same page twice.229230240.. _url-building:241242URL Building243````````````244236Flask can match URLs, but can it also generate them? Yes! To build a URL to a specific function, you can use the :func:`~flask.url_for` function. It accepts the name of the function as its first argument and any number of keyword arguments, each corresponding to a variable part of the URL rule. Unknown variable parts are appended to the URL as query parameters.245To build a URL to a specific function, use the :func:`~flask.url_for` function.246It accepts the name of the function as its first argument and any number of247keyword arguments, each corresponding to a variable part of the URL rule.248Unknown variable parts are appended to the URL as query parameters.249250Why would you want to build URLs using the URL reversing function239:func:`~flask.url_for` instead of hard-coding them into your templates? Three good reasons:251:func:`~flask.url_for` instead of hard-coding them into your templates?2522411. Reversing is often more descriptive than hard-coding the URLs. More importantly, you can change your URLs in one go instead of needing to remember to manually change hard-coded URLs.2422. URL building handles escaping of special characters and Unicode data transparently.2433. If your application is placed outside the URL root, for example, in ``/myapplication`` instead of ``/``, :func:`~flask.url_for` properly handles that for you.2531. Reversing is often more descriptive than hard-coding the URLs.2542. You can change your URLs in one go instead of needing to remember to255 manually change hard-coded URLs.2563. URL building handles escaping of special characters and Unicode data257 transparently.2584. If your application is placed outside the URL root, for example, in259 ``/myapplication`` instead of ``/``, :func:`~flask.url_for` properly260 handles that for you.261262For example, here we use the :meth:`~flask.Flask.test_request_context` method263to try out :func:`~flask.url_for`. :meth:`~flask.Flask.test_request_context`264tells Flask to behave as though it's handling a request even while we use a265Python shell. See :ref:`context-locals`. ::266246For example, here we use the :meth:`~flask.Flask.test_request_context` method to try out :func:`~flask.url_for`. :meth:`~flask.Flask.test_request_context` tells Flask to behave as though it's handling a request even while we use a Python shell. Also see, :ref:`context-locals`.267 from flask import Flask, url_for268248Example::269 app = Flask(__name__)270250 >>> from flask import Flask, url_for251 >>> app = Flask(__name__)252 >>> @app.route('/')253 ... def index(): pass254 ...255 >>> @app.route('/login')256 ... def login(): pass257 ...258 >>> @app.route('/user/<username>')259 ... def profile(username): pass260 ...261 >>> with app.test_request_context():262 ... print(url_for('index'))263 ... print(url_for('login'))264 ... print(url_for('login', next='/'))265 ... print(url_for('profile', username='John Doe'))266 ...271 @app.route('/')272 def index():273 return 'index'274275 @app.route('/login')276 def login():277 return 'login'278279 @app.route('/user/<username>')280 def profile(username):281 return '{}'s profile'.format(username)282283 with app.test_request_context():284 print(url_for('index'))285 print(url_for('login'))286 print(url_for('login', next='/'))287 print(url_for('profile', username='John Doe'))288289 /290 /login291 /login?next=/292 /user/John%20Doe293294HTTP Methods295````````````296275Web application communicate using HTTP and use different methods for accessing URLs. You should familiarize yourself with the HTTP methods as you work with Flask. By default, a route only answers to ``GET`` requests. You can use the ``methods`` argument of the :meth:`~flask.Flask.route` decorator to handle different HTTP methods. For example::297Web applications use different HTTP methods when accessing URLs. You should298familiarize yourself with the HTTP methods as you work with Flask. By default,299a route only answers to ``GET`` requests. You can use the ``methods`` argument300of the :meth:`~flask.Flask.route` decorator to handle different HTTP methods.301::302303 @app.route('/login', methods=['GET', 'POST'])304 def login():305 if request.method == 'POST':306 do_the_login()307 else:308 show_the_login_form()309284If ``GET`` is present, Flask automatically adds support for the ``HEAD`` method and handles ``HEAD`` requests according to the the `HTTP RFC`_. Likewise, as of Flask 0.6, ``OPTIONS`` is automatically implemented for you.310If ``GET`` is present, Flask automatically adds support for the ``HEAD`` method311and handles ``HEAD`` requests according to the the `HTTP RFC`_. Likewise,312``OPTIONS`` is automatically implemented for you.313314.. _HTTP RFC: https://www.ietf.org/rfc/rfc2068.txt315316Static Files
521 unmodified lines