-2

I'm having a KeyError issue when trying to access something from flask.session and I'm questioning my implementation.

Essentially, I have a PUT request that looks something like this

def auth():
    flask.session["access"] = "Admin"

blueprint.before_request(auth)
def put(...):
    ...
    if flask.session["access"] == "Admin":
        do_something_cool()

I'm getting a KeyError issue here and I suspect does it have something to do with the usage of blueprint?

Thanks

Tim
  • 2,221
  • 6
  • 22
  • 43
  • Isn't there a missing `@`? It should be `@blueprint.before_request(auth)`, because you're using the function as a decorator, imo. – colidyre Apr 06 '20 at 19:10

1 Answers1

0

flask.session is not available outside of a request context. You are missing a decorator to register your view:

import flask

bp = flask.Blueprint('auth', 'auth')

@bp.before_request
def auth(): flask.session['access'] = 'Admin'

@bp.route('/something')
def put():
    if flask.session['access'] == 'Admin': do_something_cool()
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63