404 when setting an app.route in Flask

Asked

Viewed 168 times

1

I have this simple code available on the Flask page:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

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

As the Service rises, everything works perfectly. However, by adding something to app.rote, will return a 404. Ex: app.route("/Teste")

Why does that happen ?

1 answer

2


This:

@app.route("/")
def hello():
    return "Hello World!"

Generates a route default in your website instance. When you switch to:

@app.route("/Teste")
def hello():
    return "Hello World!"

The route default there will be no more, and there will be another route called /Teste.

The recommended for your test would be you set two actions with different routes:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

@app.route("/Teste")
def teste():
    return "Oi! Isto é um teste."

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

With that, so much / how much /Teste work.

  • Got it. I took the test and it worked 100%. Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.