Reputation: 393
I have application in Python and using Flask. This is web api application without html files. In server_pi.py I have:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello world"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8082, debug=True)
Application start and when I type in browser: localhost:8082/
it shows me Hello world
But I have additional endpoints in views.py file that is placed in app folder. This is structer of my project:
rpi_server2:
-app:
-views.py
-init.py
-server_pi.py
-run.py
In views.py
I have endpoints:
@app.route('/')
@app.route('/index')
def hello():
return "Hello world"
@app.route("/index2")
def hello():
return "Hello world2"
And I can't get to this endpoints. When I type localhost:8082/index2
in the browser I get 404 error
. When I comment @app.route('/')
in the server_pi.py
the result is the same.
This is code of the sample application that I have on my RPi.
server_pi.py
:
from flask import Flask
app = Flask(__name__)
#@app.route("/turnleft")
#@app.route("/")
#def hello():
# return "Hello World!"
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8082, debug=True)
in folder app I have file init.py
:
from flask import Flask
app = Flask(__name__)
from app import views
and the second file in app folder views.py
:
from app import app
@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"
And when I type localhost:8082/index
in the browser I have 404
Upvotes: 0
Views: 953
Reputation: 382
You can handle multiple routes with a single function by simply stacking additional route decorators above any route! You cannot have more than one function with the same name You can just change the name of the second function to hello2
@app.route('/')
@app.route('/index')
def hello():
return "Hello world"
@app.route("/index2")
def hello2():
return "Hello world2"
Upvotes: 1