0

So I'm using Flask and Twilio's Whatsapp API and I want to have a list of all the routes. The usual code is:

@app.route("/whatever", methods=['GET', 'POST'])
def func():
    <Other Stuff>

But I would like something like this

routes = ['route1', 'route2' , 'route3']
for i in routes:
    @app.route("/"+i, methods=['GET', 'POST'])
    def func():
        <Other Stuff>

But when I run it, it doesn't work because the same function is being defined multiple times. Is there a work-around for this. I am new to coding and Python so any help is appreciated!

petezurich
  • 9,280
  • 9
  • 43
  • 57
ScottBot10
  • 153
  • 2
  • 10

1 Answers1

1

I think the real question you are asking is how can you map multiple routes to the same function, this can be done just just adding multiple annotations to that function:

@app.route("/route1", methods=['GET', 'POST'])
@app.route("/route2", methods=['GET', 'POST'])
@app.route("/route3", methods=['GET', 'POST'])
def func():
    <Other Stuff>
JD D
  • 7,398
  • 2
  • 34
  • 53