路由
简单来说,路由就是一个url到函数的映射,通过路由规则,可以使得url被指定的函数进行处理解析。
我们都知道现在的web系统的URL都是可以自定义的,也就是我们可以指定url和具体的业务控制器相关联,而这些就是通过路由来实现的。
flask中集成了路由处理模块,我们只需要简单地使用route装饰器就可以实现路由匹配。
@app.route('/')
def index():
return 'Index Page'
@app.route('/hello')
def hello():
return 'Hello, World'
上面的例子中,我们访问浏览器的时候,比如输入http://127.0.0.1/ 就会返回'index page',当输入http://127.0.0.1/hello 就会返回‘Hello World',这就是路由的基本使用。
@app.route('/user/')
def show_user(username):
return username
@app.route('/post/')
def show_post(post_id):
return 'Post %d' % post_id
@app.route('/path/')
def show_subpath(subpath):
# show the subpath after /path/
return 'Subpath %s&#