Skip to content
Advertisement

Flask returns 404 error when trying to connect via localhost

I am trying to build a Python Flask application, but I cannot access it. Flask recognizes the request, but for some reason doesn’t execute the function. Here is my code:

from flask import Flask, render_template

app = Flask(__name__)

app.route("/")
def main_page():
    print("main_page() was called")
    return render_template("main_page.html")

I am currently running it by typing flask run the Windows CMD. The GET request is detected, but main_page is not called and 404 is returned.

127.0.0.1 - - [01/Apr/2022 18:07:55] "GET / HTTP/1.1" 404 -

If anyone can help I would appreciate it.

Advertisement

Answer

You have missed ‘@’ in app.route, it should be

@app.route('/')

Few points, you can try debug mode, not sure with flask run. By adding main method

if __name__ == "__main__":
    app.run(debug=True)

I tried below command python file.py runserver -d

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement