setup.rst
This file
Path
docs/tutorial/setup.rst
83 lines · 3.0 KiB
Last changed by
Minor tutorial documentation fixes (grammar, etc)Adam Zapletalbefore adoption5ede530
History of this fileTake it
The walk stopped after 200 commits: lines older than 931dcc4 point at it.
Added lazyloading pattern and explicit chapter links in tutorial.Armin Ronacherbefore adoption9d19b77
lines 1–2why, one step back1.. _tutorial-setup:2Fixed a doc bug and added distribtue docs.Armin Ronacherbefore adoption931dcc4
lines 3–28why, one step back3Step 2: Application Setup Code4==============================56Now that we have the schema in place we can create the application module.7Let's call it `flaskr.py` inside the `flaskr` folder. For starters we8will add the imports we will need as well as the config section. For9small applications it's a possibility to drop the configuration directly10into the module which we will be doing here. However a cleaner solution11would be to create a separate `.ini` or `.py` file and load that or import12the values from there.1314::1516 # all the imports17 import sqlite318 from flask import Flask, request, session, g, redirect, url_for, \19 abort, render_template, flash2021 # configuration22 DATABASE = '/tmp/flaskr.db'23 DEBUG = True24 SECRET_KEY = 'development key'25 USERNAME = 'admin'26 PASSWORD = 'default'2728Next we can create our actual application and initialize it with thePorted examples over to new config. documented upgradingArmin Ronacherbefore adoptiondfecc86
line 29why, one step back29config from the same file::Fixed a doc bug and added distribtue docs.Armin Ronacherbefore adoption931dcc4
lines 30–32why, one step back3031 # create our little application :)32 app = Flask(__name__)Ported examples over to new config. documented upgradingArmin Ronacherbefore adoptiondfecc86
line 33why, one step back33 app.config.from_object(__name__)Fixed a doc bug and added distribtue docs.Armin Ronacherbefore adoption931dcc4
line 34why, one step back34Ported examples over to new config. documented upgradingArmin Ronacherbefore adoptiondfecc86
lines 35–50why, one step back35:meth:`~flask.Config.from_object` will look at the given object (if it's a36string it will import it) and then look for all uppercase variables37defined there. In our case, the configuration we just wrote a few lines38of code above. You can also move that into a separate file.3940It is also a good idea to be able to load a configuration from a41configurable file. This is what :meth:`~flask.Config.from_envvar` can42do::4344 app.config.from_envvar('FLASKR_SETTINGS', silent=True)4546That way someone can set an environment variable called47:envvar:`FLASKR_SETTINGS` to specify a config file to be loaded which will48then override the default values. The silent switch just tells Flask to49not complain if no such environment key is set.50Fixed a doc bug and added distribtue docs.Armin Ronacherbefore adoption931dcc4
lines 51–54why, one step back51The `secret_key` is needed to keep the client-side sessions secure.52Choose that key wisely and as hard to guess and complex as possible. The53debug flag enables or disables the interactive debugger. Never leave54debug mode activated in a production system because it will allow users toMinor tutorial documentation fixes (grammar, etc)Adam Zapletalbefore adoption5ede530
line 55why, one step back55execute code on the server!Fixed a doc bug and added distribtue docs.Armin Ronacherbefore adoption931dcc4
lines 56–63why, one step back5657We also add a method to easily connect to the database specified. That58can be used to open a connection on request and also from the interactive59Python shell or a script. This will come in handy later6061::6263 def connect_db():Ported examples over to new config. documented upgradingArmin Ronacherbefore adoptiondfecc86
line 64why, one step back64 return sqlite3.connect(app.config['DATABASE'])Fixed a doc bug and added distribtue docs.Armin Ronacherbefore adoption931dcc4
lines 65–66why, one step back6566Finally we just add a line to the bottom of the file that fires up theMinor tutorial documentation fixes (grammar, etc)Adam Zapletalbefore adoption5ede530
line 67why, one step back67server if we want to run that file as a standalone application::Fixed a doc bug and added distribtue docs.Armin Ronacherbefore adoption931dcc4
lines 68–81why, one step back6869 if __name__ == '__main__':70 app.run()7172With that out of the way you should be able to start up the application73without problems. When you head over to the server you will get an 40474page not found error because we don't have any views yet. But we will75focus on that a little later. First we should get the database working.7677.. admonition:: Externally Visible Server7879 Want your server to be publically available? Check out the80 :ref:`externally visible server <public-server>` section for more81 information.Added lazyloading pattern and explicit chapter links in tutorial.Armin Ronacherbefore adoption9d19b77
lines 82–83why, one step back8283Continue with :ref:`tutorial-dbinit`.