Home > database >  Getting a 404 Error During Flask Tutorial Hello World
Getting a 404 Error During Flask Tutorial Hello World

Time:01-05

I am currently working through the Flask tutorial, on the initial "Hello World" section. However, when I run it, I keep getting a 404 Error. P.S. Nothing is running on port 5000. Any thoughts? Thanks!


Code:

import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    # a simple page that says hello
    @app.route('/hello/')
    def hello():
        return 'Hello, World!'

    return app


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



Directories:

  • flaskr/
  • instance/
  • tests/
  • venv/

Tried different ports and configuring the server_name and application_root of the config file. I am new to Flask.

CodePudding user response:

That's because you have the @app.route set to /hello/, plus you don't have a base url for the server to reroute. Hence you have to type the URL in your browser as

http://127.0.0.1:5000/hello/ or localhost:5000/hello to see the output.

If you just want to see the home page you should change your @app.route as @app.route('/')

Then you can hit the url localhost:5000 and see the Hello,World! output.

  • Related