-1

I have designed a web application; https://canpl.herokuapp.com/

at the beginning of each route I use this function to get the year that the user wants to peruse;

def get_year():
    global year
    try:
        year = request.form['year']
    except:
        pass
    return year

But, I've come to realization that this is going to be a NIGHTMARE for multiple users.

What is the best solution to pass year between each route?

1 Answers1

0

You can store it as session data, for instance:

from flask import request, session

def get_year():
    try:
        session["year"] = request.form['year']
    except KeyError:
        pass
    return session.get("year") # if "year" hasn't been set yet, return None
davidism
  • 121,510
  • 29
  • 395
  • 339
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • I'm trying to give this a try. I had to install flask_session. Do you have a recommendation on how to configure the session? Because it's not currently working with app.secret_key = "testing" | app.config['SESSION_TYPE'] = 'filesystem' | Session(app) <- that's a temp secret key ;) – ToddMcCullough Feb 25 '21 at 18:41
  • This is the `session` variable from **vanilla** Flask, not flask_session. [Relevant docs](https://flask.palletsprojects.com/en/1.1.x/quickstart/#sessions) – SuperStormer Feb 25 '21 at 18:42