user3675188
user3675188

Reputation: 7419

how to intecept undefined routes

I can define many routes in flask

@app.route('/')

@app.route('/schedules/<params>', methods=["GET"])

but how to intercepted any other routes that not defined in my app.

Upvotes: 0

Views: 909

Answers (2)

Zyber
Zyber

Reputation: 1034

I've implemented a catch_all functionality that will take the path requested and try to return an html file with that name, if it fails it will redirect to index.

@WebApp.app.route('/<path:path>', methods=['GET', 'POST'])
   def catch_all(path):
      url = path+".html"
      try:
         return flask.render_template(url)
      except:
         return flask.redirect(flask.url_for('index'))

Upvotes: 0

Green
Green

Reputation: 390

How about the following ?

@app.route('/<generic>')

You may also be looking for a way to handle 404 errors

@app.errorhandler(404)

Upvotes: 3

Related Questions