Skip to content
Myr

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

2 files+34 added−1 removed

modified:docs/quickstart.rst+6 added-1 removed
36 unmodified lines
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.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.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 Flask
673 unmodified lines
modified:flask.py+28 added
804 unmodified lines
805 in the `__init__.py` file of your package like this::806807 from flask import Flask808 app = Flask(__name__)809810 .. admonition:: About the First Parameter811812 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.816817 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.821822 For example if your application is defined in `yourapplication/app.py`823 you should create it with one of the two versions below::824825 app = Flask('yourapplication')826 app = Flask(__name__.split('.')[0])827828 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 """838839 #: The class that is used for request objects. See :class:`~flask.Request`840 #: for more information.
723 unmodified lines