Zohaib
Zohaib

Reputation: 649

Flask - 404 not found

I am a newbie in the flask development this is my first program in the flask but it shows me this error:

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

& this is my code

from flask import Flask

app = Flask(__name__)


@app.route('/index')
def index():
    return 'Hello World'


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

Upvotes: 3

Views: 20351

Answers (5)

Zubin
Zubin

Reputation: 69

I had the same problem. I created the home page which ran just fine:

@app.route('/')
def hello_world():
    return 'Hello, World!'

But then when I tried making another page the next day:

@app.route('/bye/')
def bye():
    return 'Bye!'

It gave me a 404 error. So this is what I did to solve it:

Go to terminal ---> set FLASK_APP=youPythonfilename.py ------> flask run

After doing this the problem was solved. To avoid reloading everytime you need to set your debugger to ON.

if __name__ == '__name__':
    app.run(debug=True)

After you have turned debugger ON by the above code, all you need to do is save the file everytime you make changes (windows - ctrl+s).

Upvotes: 0

Rees Kennedy
Rees Kennedy

Reputation: 11

if you make the correct changes and it still doesn't work, I found out the hard way that the answer is to just save your file, and then run the program again.

Upvotes: 0

user9683713
user9683713

Reputation: 185

You have to specify the route for index page as

@app.route('/')

If you give any other name to the template, you need to have to specify the name to the route.

For eg, if the template name is "home", then you have to give it as:

@app.route('/home')

Upvotes: 1

Anon
Anon

Reputation: 1856

I think you should just go to http://localhost:5000/index or http://127.0.0.1:5000/index but if you want to make that page your code should be like that

from flask import Flask

app = Flask(__name__)


@app.route('/')
def index():
    return 'Hello World'


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

change @app.route('/index') to @app.route('/') also you should check this http://flask.pocoo.org/docs/0.12/quickstart/#routing

Upvotes: 8

Wahinya Brian
Wahinya Brian

Reputation: 1001

I came across this question while having an almost similar problem. But in my case, the app.py would be executed, for the first time, then if i try to reload my localhost, maybe numerous times after making changes on my app.py the above cited error would be generated.

To solve this, i got a solution from this link: https://stackoverflow.com/a/44950213

Basically the last execution of python would continue running even after i updated my files and my console indicated a restart. So lets say you have made 3 changes while saving each change, then in the background you will have 3 instances of python.exe running. Depending on your OS, you will need to end these processes and re-execute your app.py.

So even if you use http://127.0.0.1:5000/index yet there are still instances of previous python.exe running, it may not execute

Note: this doesnt have to be the case all the time.

Upvotes: 0

Related Questions