App Context and Request Context
The context is the 2: the app context (the current_app, the g, the no the global), and the request context (the request, the session, the no the 1). The push is the 1 (the start, the no the the), the pop is the no (the end, the teardown, the no the the), and the g is the 1 (the per the request, the no the module). The app_context is the with (the shell, the no the 1), and the teardown is the no (the 1, the no the the). This chapter covers the 2 context (the app, the request), the current_app plus g (the no the global), the app_context (the with, the no the 1), and the teardown (the 1, the no the the).
The 2 context and the g
# the 2 (the app, the request,
# the no 1, the no the the):
from flask import (
current_app, g, request)
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
# the app context: the
# current_app, the g,
# the no the request
# the request context:
# the request, the session,
# the no the 1
# the G (the 1, the per
# request, the no module,
# the no the the):
@app.route("/feed/")
def feed():
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
if "user" not in g:
# the no: the 1 (the
# no, the no, the no
# the no, the no
# the no):
g.user = current_user
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
posts = Post.query.filter_by(
user_id=g.user.id).all()
return render_template(
"feed.html", posts=posts)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the APP_CONTEXT (the with,
# the no 1, the no the the):
with app.app_context():
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
u = db.session.get(
User, 1)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
print(u.username)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no)
The 2 is the no the 1: the app context is the current_app plus the g (the no the request, the no the the), and the request context is the request plus the session (the 1, the no the the) - the g is the 1: the g.user is the no (the module, the 1, the no the the), the per is the request (the no the the, the no the 1), and the no is the cache (the 1, the no the 1, the no the the) - the app_context is the with: the shell is the no the 1 (the with app.app_context(), the no the the), and the no is the 2 (the 1, the no the the, the no the 1) - the teardown is the next: the 1 the no the the.
The teardown and the no-global
# the TEARDOWN (the 1, the no
# the the, the no the 1):
@app.teardown_appcontext
def cleanup(exc):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
db.session.remove()
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no)
@app.teardown_request
def close_db(exc):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
if "db" in g:
# the no: the no (the
# no, the no, the no
# the no, the no
# the no):
g.pop("db").close()
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the NO-GLOBAL (the 1, the
# no module, the no the the):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
The teardown is the 1: the @app.teardown_appcontext is the end (the 1, the no the the), the db.session.remove() is the no (the scoped, the 1, the no the the), and the exc is the no (the None the no, the exception the 1) - the @app.teardown_request is the 2 (the g the pop, the no the the, the no the 1). The no the global is the 1: the module the 1 (the no the 1, the no the the), the g is the 1 (the request, the no the the), and the current_app is the no (the app, the 1, the no the the) - the rule is the one: the 2 context, the current_app plus the g, the app_context the with, and the teardown the 1 - the next chapter's session is the signed the cookie the no the server.
The Working Rules
- The 2 is the no 1, the app plus the request. The app context the current_app the g (the no the request, the no the the), and the request context the request the session (the 1, the no the the) - the no the 1 (the no the 1, the no the the), and the no the the (the no the 1, the no the the).
- The g is the 1, the per request. The g.user the no the module (the 1, the no the the), and the per the request (the no the the, the no the 1) - the no the cache (the 1, the no the 1, the no the the), and the no the 2 (the no the 1, the no the the).
- The app_context is the with, the no 1. The shell the no the 1 (the with app.app_context(), the no the the), and the no the 2 (the 1, the no the the, the no the 1) - the no the 1 (the no the 1, the no the the), and the no the the (the no the 1, the no the the).
- The teardown is the 1, the no the the. The teardown_appcontext the end (the 1, the no the the), and the db.session.remove() the no (the scoped, the 1, the no the the) - the exc the no (the None the no, the exception the 1), and the teardown_request the 2 (the g the pop, the no the the).
Summary
The context is the 2: the app context the current_app the g (the no the request, the no the global, the g.user the 1 the per request, the no the module), and the request context the request the session (the 1, the no the the). The app_context is the with (the shell the no the 1, the with app.app_context(), the no the 2), and the teardown is the 1 (the teardown_appcontext the end, the db.session.remove() the scoped, the exc the None, the teardown_request the g the pop). The no the global is the 1 (the module the 1, the g the 1, the current_app the no) - the rule is the one: the 2 context, the current_app plus the g, the app_context the with, and the teardown the 1. The next chapter's session is the signed the cookie: the session the no the server, the SECRET_KEY the sign, and the Flask-Session the Redis (the 12factor, the no the 1, the no the the).