路由
使用route()
装饰器来把函数绑定到URL:
@app.route('/')
def index():
return 'Index Page'
@app.route('/hello')
def hello():
return 'Hello, World'
变量规则
通过把URL的一部分标记为<variable_name>就可以在URL中添加变量。标记的部分会做为关键字参数传递给函数。
通过使用converter:variable_name,可以选择性的加上一个转换器,为变量指定规则。例子如下:
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % escape(username)
@app.route('/post/<int:post_id>')
def show_pos(post_id):
# show the post with the given id, the id is an integer
return 'Post %d' % post_id
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
# show the subpath after /path/
return 'Subpath %s' % escape(subpath)