Skip to content
Myr

more editorial work

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

authored by Ori Livneh · committed by Ron DuPlain · parent 593b003 · Browse files · 1 file+77 added−78 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+77 added−78 removed

  • other:+77 added−78 removed

1 file+77 added−78 removed

modified:docs/quickstart.rst+77 added-78 removed
1 unmodified line
23Quickstart4==========56Eager to get started? This page gives a good introduction on how to get7started with Flask. This assumes you already have Flask installed. If8you do not, head over to the :ref:`installation` section.6Eager to get started? This page gives a good introduction to Flask. It7assumes you already have Flask installed. If you do not, head over to the8:ref:`installation` section.91011A Minimal Application12---------------------
9 unmodified lines
2223 if __name__ == '__main__':24 app.run()2526Just save it as `hello.py` or something similar and run it with your27Python interpreter. Make sure to not call your application `flask.py`28because this would conflict with Flask itself.26Just save it as `hello.py` (or something similar) and run it with your Python27interpreter. Make sure to not call your application `flask.py` because this28would conflict with Flask itself.2930::3132 $ python hello.py33 * Running on http://127.0.0.1:5000/3435Head over to `http://127.0.0.1:5000/ <http://127.0.0.1:5000/>`_, you should36see your hello world greeting.35Now head over to `http://127.0.0.1:5000/ <http://127.0.0.1:5000/>`_, and you36should see your hello world greeting.3738So what did that code do?39401. 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 (as in this example)43 you should use `__name__` because depending on whether it's started as44 application or imported as module the name will be different45 (``'__main__'`` versus the actual import name). For more information46 on this technique, have a look at the :class:`~flask.Flask` documentation.472. 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.503. Then we use the :meth:`~flask.Flask.route` decorator to tell Flask51 which URL should trigger our function.524. The function then has a name which is also used to generate URLs to53 that particular function, and returns the message we want to display in54 the user's browser.555. Finally we use the :meth:`~flask.Flask.run` function to run the56 local server with our application. The ``if __name__ == '__main__':``57 makes sure the server only runs if the script is executed directly from58 the Python interpreter and not used as imported module.42 the application's module. If you are using a single module (as in this43 example), you should use `__name__` because depending on if it's started as44 application or imported as module the name will be different (``'__main__'``45 versus the actual import name). For more information, have a look at the46 :class:`~flask.Flask` documentation.472. Next we create an instance of this class. We pass it the name of the module48 or package. This is needed so that Flask knows where to look for templates,49 static files, and so on.503. We then use the :meth:`~flask.Flask.route` decorator to tell Flask what URL51 should trigger our function.524. The function is given a name which is also used to generate URLs for that53 particular function, and returns the message we want to display in the54 user's browser.555. Finally we use the :meth:`~flask.Flask.run` function to run the local server56 with our application. The ``if __name__ == '__main__':`` makes sure the57 server only runs if the script is executed directly from the Python58 interpreter and not used as imported module.5960To stop the server, hit control-C.6162.. _public-server:
10 unmodified lines
73 :meth:`~flask.Flask.run` method to look like this::7475 app.run(host='0.0.0.0')7677 This tells your operating system to listen on a public IP.77 This tells your operating system to listen on all public IPs.787980Debug Mode81----------
9 unmodified lines
9192 app.debug = True93 app.run()9495Or pass it to `run`::95Or pass it as a parameter to run::9697 app.run(debug=True)9899Both will have exactly the same effect.99Both methods have the exact same effect.100101.. admonition:: Attention102103 Even though the interactive debugger does not work in forking environments104 (which makes it nearly impossible to use on production servers), it still105 allows the execution of arbitrary code. That makes it a major security106 risk and therefore it **must never be used on production machines**.105 allows the execution of arbitrary code. This makes it a major security risk106 and therefore it **must never be used on production machines**.107108Screenshot of the debugger in action:109110.. image:: _static/debugger.png
2 unmodified lines
113 :alt: screenshot of debugger in action114115.. admonition:: Working With Other Debuggers116117 Debuggers interfere with each other. 118 That said, you may still wish to use the debugger in a tool of your choice. 117 Debuggers interfere with each other.118 That said, you may still wish to use the debugger in a tool of your choice.119 Flask provides the following options to manage the debug process:120121 * ``debug`` - whether to enable debug mode and catch exceptinos122 * ``use_debugger`` - whether to use the internal Flask debugger123 * ``use_reloader`` - whether to reload and fork the process on exception124125 ``debug`` must be True (i.e., exceptions must caught) in order for the 125 ``debug`` must be True (i.e., exceptions must caught) in order for the126 other two options to have any value.127128 If you're using Aptana/Eclipse for debugging you'll need to set both 128 If you're using Aptana/Eclipse for debugging you'll need to set both129 ``use_debugger`` and ``use_reloader`` to False.130131 A possible useful pattern for configuration is to set the following in your 131 A possible useful pattern for configuration is to set the following in your132 config.yaml (change the block as approriate for your application, of course)::133134 FLASK:135 DEBUG: True
22 unmodified lines
158mobile devices with slower network connections. If the user can directly159go to the desired page without having to hit the index page it is more160likely they will like the page and come back next time.161162As you have seen above, the :meth:`~flask.Flask.route` decorator is used163to bind a function to a URL. Here are some basic examples::162As you have seen above, the :meth:`~flask.Flask.route` decorator is used to163bind a function to a URL. Here are some basic examples::164165 @app.route('/')166 def index():167 return 'Index Page'
1 unmodified line
169 @app.route('/hello')170 def hello():171 return 'Hello World'172173But there is more to it! You can make certain parts of the URL dynamic174and attach multiple rules to a function.173But there is more to it! You can make certain parts of the URL dynamic and174attach multiple rules to a function.175176Variable Rules177``````````````178179To add variable parts to a URL you can mark these special sections as180``<variable_name>``. Such a part is then passed as keyword argument to181your function. Optionally a converter can be specified by specifying a182rule with ``<converter:variable_name>``. Here are some nice examples::180``<variable_name>``. Such a part is then passed as keyword argument to your181function. Optionally a converter can be specified by specifying a rule with182``<converter:variable_name>``. Here are some nice examples::183184 @app.route('/user/<username>')185 def show_user_profile(username):186 # show the user profile for that user
13 unmodified lines
200=========== ===========================================201202.. admonition:: Unique URLs / Redirection Behaviour203204 Flask's URL rules are based on Werkzeug's routing module. The idea205 behind that module is to ensure nice-looking and also unique URLs based206 on behaviour coined by Apache and earlier servers.204 Flask's URL rules are based on Werkzeug's routing module. The idea behind205 that module is to ensure beautiful and unique also unique URLs based on206 precedents laid down by Apache and earlier HTTP servers.207208 Take these two rules::209210 @app.route('/projects/')
3 unmodified lines
214 @app.route('/about')215 def about():216 return 'The about page'217218 They look rather similar, the difference is the trailing slash in the219 URL *definition*. In the first case, the canonical URL for the220 `projects` endpoint has a trailing slash. It's similar to a folder in221 that sense. Accessing it without a trailing slash will cause Flask to222 redirect to the canonical URL with the trailing slash.218 Though they look rather similar, they differ in their use of the trailing219 slash in the URL *definition*. In the first case, the canonical URL for the220 `projects` endpoint has a trailing slash. In that sense, it is similar to221 a folder on a file system. Accessing it without a trailing slash will cause222 Flask to redirect to the canonical URL with the trailing slash.223224 However, in the second case the URL is defined without a slash so it225 behaves similar to a file and accessing the URL with a trailing slash226 will result in a 404 error.224 In the second case, however, the URL is defined without a trailing slash,225 rather like the pathname of a file on UNIX-like systems. Accessing the URL226 with a trailing slash will produce a 404 "Not Found" error.227228 Why is this? This allows relative URLs to continue working if users229 access the page when they forget a trailing slash. This behaviour is230 also consistent with how Apache and other servers work. Also, the URLs231 will stay unique which ensures search engines do not index the same page232 twice.228 This behavior allows relative URLs to continue working if users access the229 page when they forget a trailing slash, consistent with how with how Apache230 and other servers work. Also, the URLs will stay unique, which helps search231 engines avoid indexing the same page twice.232233234.. _url-building:235236URL Building237````````````238240If it can match URLs, can it also generate them? Of course it can. To239If it can match URLs, can Flask also generate them? Of course it can. To240build a URL to a specific function you can use the :func:`~flask.url_for`242function. It accepts the name of the function as first argument and a243number of keyword arguments, each corresponding to the variable part of244the URL rule. Unknown variable parts are appended to the URL as query245parameter. Here are some examples:241function. It accepts the name of the function as first argument and a number242of keyword arguments, each corresponding to the variable part of the URL rule.243Unknown variable parts are appended to the URL as query parameters. Here are244some examples:245246>>> from flask import Flask, url_for247>>> app = Flask(__name__)248>>> @app.route('/')
15 unmodified lines
264/login265/login?next=/266/user/John%20Doe267269(This also uses the :meth:`~flask.Flask.test_request_context` method270explained below. It basically tells Flask to think we are handling a271request even though we are not, we are in an interactive Python shell.272Have a look at the explanation below: :ref:`context-locals`.)268(This also uses the :meth:`~flask.Flask.test_request_context` method, explained269below. It tells Flask to behave as though it is handling a request, even270though were are interacting with it through a Python shell. Have a look at the271explanation below. :ref:`context-locals`).272274Why would you want to build URLs instead of hardcoding them in your273Why would you want to build URLs instead of hard-coding them into your274templates? There are three good reasons for this:2752771. reversing is often more descriptive than hardcoding the URLs. Also and278 more importantly you can change URLs in one go without having to change279 the URLs all over the place.2761. Reversing is often more descriptive than hard-coding the URLs. More277 importantly, it allows you to change URLs in one go, without having to278 remember to change URLs all over the place.2792. URL building will handle escaping of special characters and Unicode281 data transparently for you, you don't have to deal with that.2823. If your application is placed outside the URL root (so say in283 ``/myapplication`` instead of ``/``), :func:`~flask.url_for` will284 handle that properly for you.280 data transparently for you, so you don't have to deal with them.2813. If your application is placed outside the URL root (say, in282 ``/myapplication`` instead of ``/``), :func:`~flask.url_for` will handle283 that properly for you.284285286HTTP Methods287````````````288290HTTP (the protocol web applications are speaking) knows different methods291to access URLs. By default a route only answers to `GET` requests, but292that can be changed by providing the `methods` argument to the289HTTP (the protocol web applications are speaking) knows different methods for290accessing URLs. By default, a route only answers to `GET` requests, but that291can be changed by providing the `methods` argument to the292:meth:`~flask.Flask.route` decorator. Here are some examples::293294 @app.route('/login', methods=['GET', 'POST'])295 def login():
559 unmodified lines