Sinatra equivalent passage bottle

In my Flask application, I want to open the URI as follows:

http://<base_uri>/some_string

and I want to handle requests to it differently depending on whether some_string an integer or not.

With Sinatra, I can achieve this through passing ", as shown below:

 get '/:some_string' do if is_integer(:some_string) 'Your URI contains an integer' else pass # This will pass the request on the the method below which can handle it end get '/*' do 'Your URI contains some string' end 

Here, calling pass in the first route allows the second route to process the request if :some_string not an integer.

I could not find equivalent functionality in Flask. Can anyone suggest a solution in Flask?

+4
source share
1 answer

Converting types to URLs can do this for you:

 from flask import Flask import unittest app = Flask(__name__) app.debug = True @app.route('/<int:thing>') def num(thing): return 'INT' @app.route('/<thing>') def string(thing): return 'STR' class TestDispatch(unittest.TestCase): def setUp(self): self.client = app.test_client() def test_int(self): resp = self.client.get('/10') self.assertEqual("INT", resp.data) def test_str(self): resp = self.client.get('/hello') self.assertEqual("STR", resp.data) if __name__ == '__main__': unittest.main() 
+6
source

All Articles