Skip to content
Myr

setup.rst

This file

Path

docs/tutorial/setup.rst

68 lines · 2.3 KiB

Last changed by

Added lazyloading pattern and explicit chapter links in tutorial.Armin Ronacherbefore adoption9d19b77

History of this file

Take it

Added lazyloading pattern and explicit chapter links in tutorial.Armin Ronacherbefore adoption9d19b77

lines 1–2why, one step back
1.. _tutorial-setup:
2

New style for the documentation. Looks more like the website now.Armin Ronacherbefore adoptionbcf347f

lines 3–61why, one step back
3Step 2: Application Setup Code
4==============================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 the29config::3031 # create our little application :)32 app = Flask(__name__)33 app.secret_key = SECRET_KEY34 app.debug = DEBUG3536The `secret_key` is needed to keep the client-side sessions secure.37Choose that key wisely and as hard to guess and complex as possible. The38debug flag enables or disables the interactive debugger. Never leave39debug mode activated in a production system because it will allow users to40executed code on the server!4142We also add a method to easily connect to the database specified. That43can be used to open a connection on request and also from the interactive44Python shell or a script. This will come in handy later4546::4748 def connect_db():49 return sqlite3.connect(DATABASE)5051Finally we just add a line to the bottom of the file that fires up the52server if we run that file as standalone application::5354 if __name__ == '__main__':55 app.run()5657With that out of the way you should be able to start up the application58without problems. When you head over to the server you will get an 40459page not found error because we don't have any views yet. But we will60focus on that a little later. First we should get the database working.61

Merge branch 'master' into module-supportArmin Ronacherbefore adoption5144d3b

line 62why, one step back
62.. admonition:: Externally Visible Server

New style for the documentation. Looks more like the website now.Armin Ronacherbefore adoptionbcf347f

line 63why, one step back
63

Merge branch 'master' into module-supportArmin Ronacherbefore adoption5144d3b

lines 64–66why, one step back
64 Want your server to be publically available? Check out the
65 :ref:`externally visible server <public-server>` section for more66 information.

Added lazyloading pattern and explicit chapter links in tutorial.Armin Ronacherbefore adoption9d19b77

lines 67–68why, one step back
67
68Continue with :ref:`tutorial-dbinit`.