Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 125 lines (91 sloc) 4.618 kb
dfecc86 @mitsuhiko Ported examples over to new config. documented upgrading
authored
1 .. _config:
2
182ee31 @mitsuhiko Added chapter about config
authored
3 Configuration Handling
4 ======================
5
ce6e4cb @mitsuhiko 0.5 is 0.3 now, why skip numbers?
authored
6 .. versionadded:: 0.3
182ee31 @mitsuhiko Added chapter about config
authored
7
8 Applications need some kind of configuration. There are different things
9 you might want to change. Like toggling debug mode, the secret key and a
10 lot of very similar things.
11
12 The way Flask is designed usually requires the configuration to be
13 available when the application starts up. You can either hardcode the
14 configuration in the code which for many small applications is not
15 actually that bad, but there are better ways.
16
17 Independent of how you load your config, there is a config object
18 available which holds the loaded configuration values:
19 The :attr:`~flask.Flask.config` attribute of the :class:`~flask.Flask`
20 object. This is the place where Flask itself puts certain configuration
21 values and also where extensions can put their configuration values. But
22 this is also where you can have your own configuration.
23
24 Configuration Basics
25 --------------------
26
27 The :attr:`~flask.Flask.config` is actually a subclass of a dictionary and
28 can be modified just like any dictionary::
29
30 app = Flask(__name__)
31 app.config['DEBUG'] = True
32
33 Certain configuration values are also forwarded to the
34 :attr:`~flask.Flask` object so that you can read and write them from
35 there::
36
37 app.debug = True
38
39 To update multiple keys at once you can use the :meth:`dict.update`
40 method::
41
42 app.config.update(
43 DEBUG=True,
44 SECRET_KEY='...'
45 )
46
47 Builtin Configuration Values
48 ----------------------------
49
50 The following configuration values are used internally by Flask:
51
dfecc86 @mitsuhiko Ported examples over to new config. documented upgrading
authored
52 .. tabularcolumns:: |p{6.5cm}|p{8.5cm}|
53
182ee31 @mitsuhiko Added chapter about config
authored
54 =============================== =========================================
55 ``DEBUG`` enable/disable debug mode
56 ``SECRET_KEY`` the secret key
57 ``SESSION_COOKIE_NAME`` the name of the session cookie
58 ``PERMANENT_SESSION_LIFETIME`` the lifetime of a permanent session as
59 :class:`datetime.timedelta` object.
60 ``USE_X_SENDFILE`` enable/disable x-sendfile
61 =============================== =========================================
62
63 Configuring from Files
64 ----------------------
65
66 Configuration becomes more useful if you can configure from a file. And
67 ideally that file would be outside of the actual application package that
68 you can install the package with distribute (:ref:`distribute-deployment`)
69 and still modify that file afterwards.
70
71 So a common pattern is this::
72
73 app = Flask(__name__)
74 app.config.from_object('yourapplication.default_settings')
75 app.config.from_envvar('YOURAPPLICATION_SETTINGS')
76
77 What this does is first loading the configuration from the
78 `yourapplication.default_settings` module and then overrides the values
79 with the contents of the file the :envvar:`YOURAPPLICATION_SETTINGS`
80 environment variable points to. This environment variable can be set on
81 Linux or OS X with the export command in the shell before starting the
82 server::
83
84 $ export YOURAPPLICATION_SETTINGS=/path/to/settings.cfg
85 $ python run-app.py
86 * Running on http://127.0.0.1:5000/
87 * Restarting with reloader...
88
89 On Windows systems use the `set` builtin instead::
90
91 >set YOURAPPLICATION_SETTINGS=\path\to\settings.cfg
92
93 The configuration files themselves are actual Python files. Only values
94 in uppercase are actually stored in the config object later on. So make
95 sure to use uppercase letters for your config keys.
96
97 Here an example configuration file::
98
99 DEBUG = False
100 SECRET_KEY = '?\xbf,\xb4\x8d\xa3"<\x9c\xb0@\x0f5\xab,w\xee\x8d$0\x13\x8b83'
101
102 Make sure to load the configuration very early on so that extensions have
103 the ability to access the configuration when starting up. There are other
104 methods on the config object as well to load from individual files. For a
105 complete reference, read the :class:`~flask.Config` object's
106 documentation.
107
108
109 Configuration Best Practices
110 ----------------------------
111
112 The downside with the approach mentioned earlier is that it makes testing
113 a little harder. There is no one 100% solution for this problem in
114 general, but there are a couple of things you can do to improve that
115 experience:
116
117 1. create your application in a function and register modules on it.
118 That way you can create multiple instances of your application with
119 different configurations attached which makes unittesting a lot
120 easier. You can use this to pass in configuration as needed.
121
122 2. Do not write code that needs the configuration at import time. If you
123 limit yourself to request-only accesses to the configuration you can
124 reconfigure the object later on as needed.
Something went wrong with that request. Please try again.