@app.route('/user/<username>')defshow_user_profile(username):# show the user profile for that userreturn'User %s' % username
@app.route('/post/<int:post_id>')defshow_post(post_id):# show the post with the given id, the id is an integerreturn'Post %d' % post_id
```
## 程序请求上下文### Flask从客户端收到请求,需让视图函数能访问一些对象,才能处理请求。为了避免大量可有可无的参数把视图函数弄的一团糟,Flask使用上下文把某些对象变为全局可访问。### 如:
```Python
from Flask import request
@app.route('/')defindex():
user=request.headers.get('User-Agent')
return'your browser is %s' % user
HTTP 方法
HTTP (与 Web 应用会话的协议)有许多不同的访问 URL 方法。默认情况下,路由只回应 GET 请求,但是通过 route() 装饰器传递 methods 参数可以改变这个行为。这里有一些例子: