Flask: RuntimeError: Working outside of application context.

I am currently following a Flask tutorial, and I keep getting this error when I try to make a SQL database

RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that
needed the current application. To solve this, set up an application
context with app.app_context(). See the documentation for more

I have checked online for solutions, but none of them work for me.

from flask import Flask, redirect, url_for, render_template, request, session, flash, current_app 
from datetime import timedelta 
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.secret_key = "hello" 
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.sqlite3'
app.permanent_session_lifetime = timedelta(days=1)

db = SQLAlchemy(app)

class users(db.Model):
    _id = db.Column("id", db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    email = db.Column(db.String(100))

    def __init__(self, name, email): = name = email

def home():
    return render_template('index.html')

@app.route('/login', methods=['POST', 'GET']) 
def login():
    if request.method == "POST":
        session.permanent = True
        user = request.form["nm"] 
        session["user"] = user 
        flash("Login successful.", "info")

        return redirect(url_for("user")) 

        if "user" in session:
            flash("Already logged in.")
            return redirect((url_for("user")))
        return render_template("login.html")

@app.route('/user', methods=["POST", "GET"]) 
def user():
    email = None
    if "user" in session:
        user = session["user"]
        if request.method == "POST":
            email = request.form["email"]
            session["email"] = email
            flash("Email was saved!")
            if "email" in session:
                email = session["email"]
        return render_template("user.html", email=email)
        flash("Not logged in.")
        return redirect(url_for("login"))

def logout():
    flash("You have been logged out", "info")
    session.pop("user", None)
    session.pop("email", None)
    return redirect((url_for("login")))

if __name__ == "__main__":

When exactly are you getting this error?