404 when setting an app.route in Flask

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()

When you upload the Service, everything works perfectly. But by adding something in app.rote , it will return a 404. Ex: app.route("/Teste")

Why does this happen?

    
asked by anonymous 14.07.2016 / 22:25

1 answer

2

This:

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

Generate a default route on your site instance. When you change to:

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

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

Recommended for your test would be to define 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 this, both / and /Teste work.

    
14.07.2016 / 22:35