@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % username
@app.route('/post/<int:post_id>')
def show_post(post_id):
# show the post with the given id, the id is an integer
return 'Post %d' % post_id
现有的转换器有:
int 接受整数
float 接受浮点数
path 和缺省情况相同,但也接受斜杠
使用变量路由规则, 自己内部增加判断
通过把 URL 的一部分标记为 <variable_name> 就可以在 URL 中添加变量。标记的 部分会作为关键字参数传递给函数。通过使用 <converter:variable_name> ,可以 选择性的加上一个转换器,为变量指定规则。请看下面的例子:
楼上的方法只是说,相同的需求让你通过动态url来操作函数去拿到这个请求然后处理。
的确可以达到少写view函数的目的。
楼主问题并不是很清楚。
路由规则并不是一个路由路径对应一个页面的意思,你可以在路由路径里去判断应该做何种模板的渲染,也可以通过POST GET来分别渲染不同的展示模板,等等。一个路径对应一个页面的思考方式是不正确的,希望楼主能顿悟,加油