diff --git a/docs/tutorial/setup.rst b/docs/tutorial/setup.rst index acf227f9..ed8a744c 100644 --- a/docs/tutorial/setup.rst +++ b/docs/tutorial/setup.rst @@ -27,7 +27,7 @@ config from the same file, in `flaskr.py`:: # Load default config and override config from an environment variable app.config.update(dict( - DATABASE='/tmp/flaskr.db', + DATABASE=os.path.join(app.root_path, 'flaskr.db'), DEBUG=True, SECRET_KEY='development key', USERNAME='admin', @@ -38,11 +38,20 @@ config from the same file, in `flaskr.py`:: The :class:`~flask.Config` object works similar to a dictionary so we can update it with new values. -.. admonition:: Windows +.. admonition:: Database Path - If you are on Windows, replace `/tmp/flaskr.db` with a different writeable - path of your choice, in the configuration and for the rest of this - tutorial. + Operating systems know the concept of a current working directory for + each process. Unfortunately you cannot depend on this in web + applications because you might have more than one application in the + same process. + + For this reason the ``app.root_path`` attribute can be used to + get the path to the application. Together with the ``os.path`` module + files can then easily be found. In this example we place the + database right next to it. + + For a real-work application it's recommended to use + :ref:`instance-folders` instead. Usually, it is a good idea to load a separate, environment specific configuration file. Flask allows you to import multiple configurations and it diff --git a/examples/flaskr/flaskr.py b/examples/flaskr/flaskr.py index 65622c01..384162af 100644 --- a/examples/flaskr/flaskr.py +++ b/examples/flaskr/flaskr.py @@ -10,6 +10,7 @@ :license: BSD, see LICENSE for more details. """ +import os from sqlite3 import dbapi2 as sqlite3 from flask import Flask, request, session, g, redirect, url_for, abort, \ render_template, flash @@ -20,7 +21,7 @@ app = Flask(__name__) # Load default config and override config from an environment variable app.config.update(dict( - DATABASE='/tmp/flaskr.db', + DATABASE=os.path.join(app.root_path, 'flaskr.db'), DEBUG=True, SECRET_KEY='development key', USERNAME='admin',