How to create first_or_redirect() like first_or_404() in flask application

Solution
from functools import wraps

from flask import Flask, redirect, url_for, session


app = Flask(__name__)

app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'


def wrapper_redirect(f):
    @wraps(f)
    def wrapped_func(*args, **kwargs):
        if not session.get("login"):
            return redirect(url_for("index"))
        else:
            return f(*args, **kwargs)
    return wrapped_func


@app.route("/")
def index():
    session["login"] = True  # log in

    return "

index page

" @app.route("/test") @wrapper_redirect def hello_world(): if session.get("login"): del session["login"] # log out return "

Hello, World!

" if __name__ == '__main__': app.run(host="0.0.0.0", port=9999)