-1

I have a flask web app and I wanted a function to be called every time the page loads. I got it to work using "@app.before_request", my only problem is, I have 4 requests that are being made on every page load.

Here's my logs in my console

127.0.0.1 - - [14/Jun/2022 17:54:47] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [14/Jun/2022 17:54:48] "GET /static/style.css HTTP/1.1" 304 -
127.0.0.1 - - [14/Jun/2022 17:54:48] "GET /static/profile.jpg HTTP/1.1" 304 -
127.0.0.1 - - [14/Jun/2022 17:54:48] "GET /favicon.ico HTTP/1.1" 404 -

Obviously it's running before every single request, including requests just for loading my html and css files. I want to limit it so that it only runs once and not 4 times. I'm having it add +1 to a count on a database and since 4 requests are being run, it's running 4 times and adding 4 every time.

Here's my before_request class and function, that I want to somehow limit to running only once (maybe the initial request) and not all 4

@app.before_request
def before_request():
    dbcounter = handler()
    print(dbcounter)


@app.route('/')

def home():
    count = handler()
    return render_template("index.html")
    
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)
MP32
  • 573
  • 1
  • 9
  • 25

1 Answers1

1

@app.before_first_request is what solved it!

MP32
  • 573
  • 1
  • 9
  • 25