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
Provenance
- Commit
2592f92- Tree
80ac589- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
dfb03c5
1 file changed+83 added−55 removed
modified:docs/quickstart.rst+83 added-55 removed
157 unmodified lines158 159 Routing160 -------161 162-Modern 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. 162+Modern web applications use meaningful URLs to help users. Users are more163+likely to like a page and come back if the page uses a meaningful URL they can164+remember and use to directly visit a page.165 164-As 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::166+Use the :meth:`~flask.Flask.route` decorator to bind a function to a URL. ::167 168 @app.route('/')169 def index():170 return 'Index Page'1 unmodified line172 @app.route('/hello')173 def hello():174 return 'Hello, World'175 174-You can do more! You can make parts of the URL dynamic and attach multiple rules to a function.176+You can do more! You can make parts of the URL dynamic and attach multiple177+rules to a function.178 179 Variable Rules180 ``````````````181 179-You 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::182+You can add variable sections to a URL by marking sections with183+``<variable_name>``. Your function then receives the ``<variable_name>``184+as a keyword argument. Optionally, you can use a converter to specify the type185+of the argument like ``<converter:variable_name>``. ::186 187 @app.route('/user/<username>')188 def show_user_profile(username):189 # show the user profile for that user10 unmodified lines200 return 'Subpath %s' % subpath201 202 Converter types:203 198-=========== ===============================================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+========== ==========================================211 207-.. admonition:: Unique URLs / Redirection Behavior212+Unique URLs / Redirection Behavior213+``````````````````````````````````214 209- 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.215+Take these two rules::216 211- Take these two rules::217+ @app.route('/projects/')218+ def projects():219+ return 'The project page'220 213- @app.route('/projects/')214- def projects():215- return 'The project page'221+ @app.route('/about')222+ def about():223+ return 'The about page'224 217- @app.route('/about')218- def about():219- return 'The about page'225+Though they look similar, they differ in their use of the trailing slash in226+the URL. In the first case, the canonical URL for the ``projects`` endpoint227+uses a trailing slash. It's similar to a folder in a file system; if you228+access the URL without a trailing slash, Flask redirects you to the229+canonical URL with the trailing slash.230 221- 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.231+In the second case, however, the URL definition lacks a trailing slash,232+like the pathname of a file on UNIX-like systems. Accessing the URL with a233+trailing slash produces a 404 “Not Found” error.234 223- 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.235+This behavior allows relative URLs to continue working even if the trailing236+slash is omitted, consistent with how Apache and other servers work. Also,237+the URLs will stay unique, which helps search engines avoid indexing the238+same page twice.239 225- 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.229-230-240 .. _url-building:241 242 URL Building243 ````````````244 236-Flask 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.245+To build a URL to a specific function, use the :func:`~flask.url_for` function.246+It accepts the name of the function as its first argument and any number of247+keyword arguments, each corresponding to a variable part of the URL rule.248+Unknown variable parts are appended to the URL as query parameters.249 250 Why 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?252 241-1. 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.242-2. URL building handles escaping of special characters and Unicode data transparently.243-3. If your application is placed outside the URL root, for example, in ``/myapplication`` instead of ``/``, :func:`~flask.url_for` properly handles that for you.253+1. Reversing is often more descriptive than hard-coding the URLs.254+2. You can change your URLs in one go instead of needing to remember to255+ manually change hard-coded URLs.256+3. URL building handles escaping of special characters and Unicode data257+ transparently.258+4. If your application is placed outside the URL root, for example, in259+ ``/myapplication`` instead of ``/``, :func:`~flask.url_for` properly260+ handles that for you.261 262+For example, here we use the :meth:`~flask.Flask.test_request_context` method263+to try out :func:`~flask.url_for`. :meth:`~flask.Flask.test_request_context`264+tells Flask to behave as though it's handling a request even while we use a265+Python shell. See :ref:`context-locals`. ::266 246-For 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_for268 248-Example::269+ app = Flask(__name__)270 250- >>> 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'274+275+ @app.route('/login')276+ def login():277+ return 'login'278+279+ @app.route('/user/<username>')280+ def profile(username):281+ return '{}'s profile'.format(username)282+283+ 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'))288+289 /290 /login291 /login?next=/292 /user/John%20Doe293 294 HTTP Methods295 ````````````296 275-Web 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::297+Web applications use different HTTP methods when accessing URLs. You should298+familiarize yourself with the HTTP methods as you work with Flask. By default,299+a route only answers to ``GET`` requests. You can use the ``methods`` argument300+of the :meth:`~flask.Flask.route` decorator to handle different HTTP methods.301+::302 303 @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()309 284-If ``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.310+If ``GET`` is present, Flask automatically adds support for the ``HEAD`` method311+and handles ``HEAD`` requests according to the the `HTTP RFC`_. Likewise,312+``OPTIONS`` is automatically implemented for you.313 314 .. _HTTP RFC: https://www.ietf.org/rfc/rfc2068.txt315 316 Static Files521 unmodified lines