flask route

app.route

路由传参的5中方法

@app.route('/user/')   # 没有参数
@app.route('/user/<username>')   # 字符串参数
@app.route('/post/<int:post_id>')  # 整形参数
@app.route('/post/<float:post_id>')  # 浮点参数
@app.route('/post/<path:path>')  # 路径参数

例子

@app.route('/')
def index():
    pass

@app.route('/<username>')
def show_user(username):
    pass

@app.route('/post/<int:post_id>')
def show_post(post_id):
    pass

一个函数同时定义多个规则

@app.route('/users/', defaults={'page': 1})
@app.route('/users/page/<int:page>')
def show_users(page):
    pass

app.add_url_rule

@app.routeapp.add_url_rule参数
add_url_rule(rule, endpoint=None, view_func=None, provide_automatic_options=None, **options)
class werkzeug.routing.Rule(string, defaults=None, subdomain=None, methods=None, build_only=False, endpoint=None, strict_slashes=None, redirect_to=None, alias=False, host=None)

参数描述
ruleURL规则
view_func函数名称,修饰符不用添加
defaultsURL中的默认参数defaults={'k':'v'}
endpoint=Nonethe endpoint for the registered URL rule,itself assumes the name of the view function as. Flask,反向生成URLurl_for('名称'),否则url_for()默认使用函数名称
methods=None请求方法method=["GET","POST"],默认为get,不添加则不能使用
strict_slashes=None对URL最后的 / 符号是否严格要求
redirect_to=None重定向到指定地址
subdomain=None子域名访问
@app.route('/')
def index():
    pass

# 等价于

def index():
    pass
app.add_url_rule('index', '/')
app.view_functions['index'] = index

methods

methods=['GET', 'POST']

如果不声明methods,则此页面只能接收get请求,拒绝post请求。

参考文献:
http://flask.pocoo.org/docs/0.12/api/#url-route-registrations
https://www.cnblogs.com/wupeiqi/articles/7552008.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值