Proofread docs/quickstart
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Aku Kotkavuo · committed by Armin Ronacher · parent 738c66e · Browse files · 1 file·+79 added−82 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+79 added−82 removed
- other:+79 added−82 removed
Provenance
- Commit
43ae651- Tree
4d37c1e- Author
Aku Kotkavuo <aku@hibana.net>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
738c66e
1 file changed+79 added−82 removed
modified:docs/quickstart.rst+79 added-82 removed
1 unmodified line2 3 Quickstart4 ==========5 6-Eager to get started? This page gives a good introduction in how to gets6+Eager to get started? This page gives a good introduction in how to get7 started with Flask. This assumes you already have Flask installed. If8 you do not, head over to the :ref:`installation` section.9 10 25 unmodified lines36 see your hello world greeting.37 38 So what did that code do?39 40-1. first we imported the :class:`~flask.Flask` class. An instance of this40+1. First we imported the :class:`~flask.Flask` class. An instance of this41 class will be our WSGI application. The first argument is the name of42 the application's module. If you are using a single module (like here)43 you should use `__name__` because depending on if it's started as44 application or imported as module the name will be different45 (``'__main__'`` versus the actual import name). For more information46 on that, have a look at the :class:`~flask.Flask` documentation.47-2. next we create an instance of it. We pass it the name of the module /47+2. Next we create an instance of it. We pass it the name of the module /48 package. This is needed so that Flask knows where it should look for49 templates, static files and so on.50 3. Then we use the :meth:`~flask.Flask.route` decorator to tell Flask51 what URL should trigger our function.28 unmodified lines80 81 Debug Mode82 ----------83 84-Now that :meth:`~flask.Flask.run` method is nice to start a local84+The :meth:`~flask.Flask.run` method is nice to start a local85 development server, but you would have to restart it manually after each86 change you do to code. That is not very nice and Flask can do better. If87 you enable the debug support the server will reload itself on code changes88 and also provide you with a helpful debugger if things go wrong.11 unmodified lines100 Both will have exactly the same effect.101 102 .. admonition:: Attention103 104- The interactive debugger however does not work in forking environments105- which makes it nearly impossible to use on production servers but the106- debugger still allows the execution of arbitrary code which makes it a107- major security risk and **must never be used on production machines**108- because of that.104+ Even though the interactive debugger does not work in forking environments105+ (which makes it nearly impossible to use on production servers), it still106+ allows the execution of arbitrary code. That makes it a major security107+ risk and therefore it **must never be used on production machines**.108 109 Screenshot of the debugger in action:110 111 .. image:: _static/debugger.png4 unmodified lines116 117 Routing118 -------119 120+Modern web applications have beautiful URLs. This helps people remember121+the URLs which is especially handy for applications that are used from122+mobile devices with slower network connections. If the user can directly123+go to the desired page without having to hit the index page it is more124+likely he will like the page and come back next time.125+126 As you have seen above, the :meth:`~flask.Flask.route` decorator is used122-to bind a function to a URL. But there is more to it! You can make123-certain parts of the URL dynamic and attach multiple rules to a function.127+to bind a function to a URL. Here are some basic examples::128 125-Here some examples::126-129 @app.route('/')130 def index():131 return 'Index Page'132 133 @app.route('/hello')134 def hello():135 return 'Hello World'136 137+But there is more to it! You can make certain parts of the URL dynamic138+and attach multiple rules to a function.139 140 Variable Rules141 ``````````````142 139-Modern web applications have beautiful URLs. This helps people remember140-the URLs which is especially handy for applications that are used from141-mobile devices with slower network connections. If the user can directly142-go to the desired page without having to hit the index page it is more143-likely he will like the page and come back next time.144-143 To add variable parts to a URL you can mark these special sections as144 ``<variable_name>``. Such a part is then passed as keyword argument to145 your function. Optionally a converter can be specified by specifying a148-rule with ``<converter:variable_name>``. Here some nice examples::146+rule with ``<converter:variable_name>``. Here are some nice examples::147 148 @app.route('/user/<username>')149 def show_user_profile(username):150 # show the user profile for that user49 unmodified lines200 201 URL Building202 ````````````203 206-If it can match URLs, can it also generate them? Of course you can. To204+If it can match URLs, can it also generate them? Of course it can. To205 build a URL to a specific function you can use the :func:`~flask.url_for`206 function. It accepts the name of the function as first argument and a207 number of keyword arguments, each corresponding to the variable part of208 the URL rule. Unknown variable parts are appended to the URL as query211-parameter. Here some examples:209+parameter. Here are some examples:210 211 >>> from flask import Flask, url_for212 >>> app = Flask(__name__)213 >>> @app.route('/')39 unmodified lines253 254 HTTP (the protocol web applications are speaking) knows different methods255 to access URLs. By default a route only answers to `GET` requests, but256 that can be changed by providing the `methods` argument to the259-:meth:`~flask.Flask.route` decorator. Here some examples::257+:meth:`~flask.Flask.route` decorator. Here are some examples::258 259 @app.route('/login', methods=['GET', 'POST'])260 def login():261 if request.method == 'POST':7 unmodified lines269 protocol) demands, so you can completely ignore that part of the HTTP270 specification. Likewise as of Flask 0.6, `OPTIONS` is implemented for you271 as well automatically.272 275-You have no idea what an HTTP method is? Worry not, here quick276-introduction in HTTP methods and why they matter:273+You have no idea what an HTTP method is? Worry not, here is a quick274+introduction to HTTP methods and why they matter:275 276 The HTTP method (also often called "the verb") tells the server what the277 clients wants to *do* with the requested page. The following methods are278 very common:279 280 `GET`283- The Browser tells the server: just *get* me the information stored on284- that page and send them to me. This is probably the most common285- method.281+ The browser tells the server to just *get* the information stored on282+ that page and send it. This is probably the most common method.283 284 `HEAD`288- The Browser tells the server: get me the information, but I am only285+ The browser tells the server to get the information, but it is only286 interested in the *headers*, not the content of the page. An287 application is supposed to handle that as if a `GET` request was291- received but not deliver the actual contents. In Flask you don't have292- to deal with that at all, the underlying Werkzeug library handles that293- for you.288+ received but to not deliver the actual content. In Flask you don't289+ have to deal with that at all, the underlying Werkzeug library handles290+ that for you.291 292 `POST`293 The browser tells the server that it wants to *post* some new294 information to that URL and that the server must ensure the data is2 unmodified lines297 298 `PUT`299 Similar to `POST` but the server might trigger the store procedure300 multiple times by overwriting the old values more than once. Now you304- might be asking why this is any useful, but there are some good305- reasons to do that. Consider the connection is lost during306- transmission, in that situation a system between the browser and the307- server might sent the request safely a second time without breaking301+ might be asking why is this useful, but there are some good reasons302+ to do it this way. Consider that the connection gets lost during303+ transmission: in this situation a system between the browser and the304+ server might receive the request safely a second time without breaking305 things. With `POST` that would not be possible because it must only306 be triggered once.307 308 `DELETE`312- Remove the information that the given location.309+ Remove the information at the given location.310 311 `OPTIONS`315- Provides a quick way for a requesting client to figure out which316- methods are supported by this URL. Starting with Flask 0.6, this317- is implemented for you automatically.312+ Provides a quick way for a client to figure out which methods are313+ supported by this URL. Starting with Flask 0.6, this is implemented314+ for you automatically.315 316 Now the interesting part is that in HTML4 and XHTML1, the only methods a320-form might submit to the server are `GET` and `POST`. But with JavaScript321-and future HTML standards you can use other methods as well. Furthermore322-HTTP became quite popular lately and there are more things than browsers323-that are speaking HTTP. (Your revision control system for instance might324-speak HTTP)317+form can submit to the server are `GET` and `POST`. But with JavaScript318+and future HTML standards you can use the other methods as well. Furthermore319+HTTP has become quite popular lately and browsers are no longer the only320+clients that are using HTTP. For instance, many revision control system321+use it.322 323 .. _HTTP RFC: http://www.ietf.org/rfc/rfc2068.txt324 325 Static Files53 unmodified lines379 to the :ref:`templating` section of the documentation or the official380 `Jinja2 Template Documentation381 <http://jinja.pocoo.org/2/documentation/templates>`_ for more information.382 386-Here an example template:383+Here is an example template:384 385 .. sourcecode:: html+jinja386 387 <!doctype html>19 unmodified lines407 markup to HTML) you can mark it as safe by using the408 :class:`~jinja2.Markup` class or by using the ``|safe`` filter in the409 template. Head over to the Jinja 2 documentation for more examples.410 414-Here a basic introduction in how the :class:`~jinja2.Markup` class works:411+Here is a basic introduction to how the :class:`~jinja2.Markup` class works:412 413 >>> from flask import Markup414 >>> Markup('<strong>Hello %s!</strong>') % '<blink>hacker</blink>'415 Markup(u'<strong>Hello <blink>hacker</blink>!</strong>')5 unmodified lines421 .. versionchanged:: 0.5422 423 Autoescaping is no longer enabled for all templates. The following424 extensions for templates trigger autoescaping: ``.html``, ``.htm``,428- ``.xml``, ``.xhtml``. Templates loaded from string will have425+ ``.xml``, ``.xhtml``. Templates loaded from a string will have426 autoescaping disabled.427 431-.. [#] Unsure what that :class:`~flask.g` object is? It's something you432- can store information on yourself, check the documentation of that433- object (:class:`~flask.g`) and the :ref:`sqlite3` for more428+.. [#] Unsure what that :class:`~flask.g` object is? It's something in which429+ you can store information for your own needs, check the documentation of430+ that object (:class:`~flask.g`) and the :ref:`sqlite3` for more431 information.432 433 434 Accessing Request Data15 unmodified lines450 451 If you want to understand how that works and how you can implement452 tests with context locals, read this section, otherwise just skip it.453 457-Certain objects in Flask are global objects, but not just a standard458-global object, but actually a proxy to an object that is local to a459-specific context. What a mouthful. But that is actually quite easy to460-understand.454+Certain objects in Flask are global objects, but not of the usual kind.455+These objects are actually proxies to objects that are local to a specific456+context. What a mouthful. But that is actually quite easy to understand.457 458 Imagine the context being the handling thread. A request comes in and the459 webserver decides to spawn a new thread (or something else, the460 underlying object is capable of dealing with other concurrency systems3 unmodified lines464 It does that in an intelligent way that one application can invoke another465 application without breaking.466 467 So what does this mean to you? Basically you can completely ignore that472-this is the case unless you are unittesting or something different. You468+this is the case unless you are doing something like unittesting. You469 will notice that code that depends on a request object will suddenly break470 because there is no request object. The solution is creating a request471 object yourself and binding it to the context. The easiest solution for472 unittesting is by using the :meth:`~flask.Flask.test_request_context`473 context manager. In combination with the `with` statement it will bind a478-test request so that you can interact with it. Here an example::474+test request so that you can interact with it. Here is an example::475 476 from flask import request477 478 with app.test_request_context('/hello', method='POST'):13 unmodified lines492 The Request Object493 ``````````````````494 495 The request object is documented in the API section and we will not cover500-it here in detail (see :class:`~flask.request`), but just mention some of501-the most common operations. First of all you have to import it from the496+it here in detail (see :class:`~flask.request`). Here is a broad overview of497+some of the most common operations. First of all you have to import it from498 the `flask` module::499 500 from flask import request501 502 The current request method is available by using the503 :attr:`~flask.request.method` attribute. To access form data (data504 transmitted in a `POST` or `PUT` request) you can use the509-:attr:`~flask.request.form` attribute. Here a full example of the two505+:attr:`~flask.request.form` attribute. Here is a full example of the two506 attributes mentioned above::507 508 @app.route('/login', methods=['POST', 'GET'])509 def login():19 unmodified lines529 searchword = request.args.get('q', '')530 531 We recommend accessing URL parameters with `get` or by catching the532 `KeyError` because users might change the URL and presenting them a 400537-bad request page in that case is a bit user unfriendly.533+bad request page in that case is not user friendly.534 539-For a full list of methods and attributes on that object, head over to the540-:class:`~flask.request` documentation.535+For a full list of methods and attributes of the request object, head over536+to the :class:`~flask.request` documentation.537 538 539 File Uploads540 ````````````541 546-Obviously you can handle uploaded files with Flask just as easy. Just547-make sure not to forget to set the ``enctype="multipart/form-data"``548-attribute on your HTML form, otherwise the browser will not transmit your549-files at all.542+You can handle uploaded files with Flask easily. Just make sure not to543+forget to set the ``enctype="multipart/form-data"`` attribute on your HTML544+form, otherwise the browser will not transmit your files at all.545 546 Uploaded files are stored in memory or at a temporary location on the547 filesystem. You can access those files by looking at the548 :attr:`~flask.request.files` attribute on the request object. Each549 uploaded file is stored in that dictionary. It behaves just like a550 standard Python :class:`file` object, but it also has a551 :meth:`~werkzeug.FileStorage.save` method that allows you to store that557-file on the filesystem of the server. Here a simple example how that558-works::552+file on the filesystem of the server. Here is a simple example showing how553+that works::554 555 from flask import request556 557 @app.route('/upload', methods=['GET', 'POST'])36 unmodified lines594 Redirects and Errors595 --------------------596 597 To redirect a user to somewhere else you can use the603-:func:`~flask.redirect` function, to abort a request early with an error604-code the :func:`~flask.abort` function. Here an example how this works::598+:func:`~flask.redirect` function. To abort a request early with an error599+code use the :func:`~flask.abort` function. Here an example how this works::600 601 from flask import abort, redirect, url_for602 603 @app.route('/')71 unmodified lines675 676 The here mentioned :func:`~flask.escape` does escaping for you if you are677 not using the template engine (like in this example).678 684-.. admonition:: How to generate good Secret Keys679+.. admonition:: How to generate good secret keys680 681 The problem with random is that it's hard to judge what random is. And682 a secret key should be as random as possible. Your operating system683 has ways to generate pretty random stuff based on a cryptographic25 unmodified lines709 -------710 711 .. versionadded:: 0.3712 718-Sometimes you might be in the situation where you deal with data that719-should be correct, but actually is not. For example you have some client720-side code that sends an HTTP request to the server, and it's obviously713+Sometimes you might be in a situation where you deal with data that714+should be correct, but actually is not. For example you may have some client715+side code that sends an HTTP request to the server but it's obviously716 malformed. This might be caused by a user tempering with the data, or the722-client code failed. Most the time, it's okay to reply with ``400 Bad723-Request`` in that situation, but other times it is not and the code has to724-continue working.717+client code failing. Most of the time, it's okay to reply with ``400 Bad718+Request`` in that situation, but sometimes that won't do and the code has719+to continue working.720 726-Yet you want to log that something fishy happened. This is where loggers727-come in handy. As of Flask 0.3 a logger is preconfigured for you to use.721+You may still want to log that something fishy happened. This is where722+loggers come in handy. As of Flask 0.3 a logger is preconfigured for you723+to use.724 725 Here are some example log calls::726 727 app.logger.debug('A value for debugging')728 app.logger.warning('A warning occurred (%d apples)', 42)729 app.logger.error('An error occurred')730 731 The attached :attr:`~flask.Flask.logger` is a standard logging736-:class:`~logging.Logger`, so head over to the official stdlib737-documentation for more information.732+:class:`~logging.Logger`, so head over to the official `logging733+documentation <http://docs.python.org/library/logging.html>`_ for more734+information.735 736 Hooking in WSGI Middlewares737 ---------------------------738 7 unmodified lines