Explained Flask constructor better. This fixes #70
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 bc662a5 · Browse files · 2 files·+34 added−1 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+34 added−1 removed
- other:+34 added−1 removed
Provenance
- Commit
3ab318a- Tree
19fb689- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
bc662a5
2 files changed+34 added−1 removed
modified:docs/quickstart.rst+6 added-1 removed
36 unmodified lines37 38 So what did that code do?39 40 1. first we imported the :class:`~flask.Flask` class. An instance of this41- class will be our WSGI application.41+ 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 /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 Flask673 unmodified linesmodified:flask.py+28 added
804 unmodified lines805 in the `__init__.py` file of your package like this::806 807 from flask import Flask808 app = Flask(__name__)809+810+ .. admonition:: About the First Parameter811+812+ The idea of the first parameter is to give Flask an idea what813+ belongs to your application. This name is used to find resources814+ on the file system, can be used by extensions to improve debugging815+ information and a lot more.816+817+ So it's important what you provide there. If you are using a single818+ module, `__name__` is always the correct value. If you however are819+ using a package, it's usually recommended to hardcode the name of820+ your package there.821+822+ For example if your application is defined in `yourapplication/app.py`823+ you should create it with one of the two versions below::824+825+ app = Flask('yourapplication')826+ app = Flask(__name__.split('.')[0])827+828+ Why is that? The application will work even with `__name__`, thanks829+ to how resources are looked up. However it will make debugging more830+ painful. Certain extensions can make assumptions based on the831+ import name of your application. For example the Flask-SQLAlchemy832+ extension will look for the code in your application that triggered833+ an SQL query in debug mode. If the import name is not properly set834+ up, that debugging information is lost. (For example it would only835+ pick up SQL queries in `yourapplicaiton.app` and not836+ `yourapplication.views.frontend`)837 """838 839 #: The class that is used for request objects. See :class:`~flask.Request`840 #: for more information.723 unmodified lines