flask框架(五): @app.route和app.add_url_rule参数

 @app.route和app.add_url_rule参数:
            rule,                       URL规则
            view_func,                  视图函数名称
            defaults=None,              默认值,当URL中无参数,函数需要参数时,使用defaults={'k':'v'}为函数提供参数
            endpoint=None,              名称,用于反向生成URL,即: url_for('名称')
            methods=None,               允许的请求方式,如:["GET","POST"]
            

            strict_slashes=None,        对URL最后的 / 符号是否严格要求,
                                        如:
                                            @app.route('/index',strict_slashes=False),
                                                访问 http://www.xx.com/index/ 或 http://www.xx.com/index均可
                                            @app.route('/index',strict_slashes=True)
                                                仅访问 http://www.xx.com/index 
            redirect_to=None,           重定向到指定地址
                                        如:
                                            @app.route('/index/<int:nid>', redirect_to='/home/<nid>')
                                            或
                                            def func(adapter, nid):
                                                return "/home/888"
                                            @app.route('/index/<int:nid>', redirect_to=func)
            subdomain=None,             子域名访问
                                                from flask import Flask, views, url_for

                                                app = Flask(import_name=__name__)
                                                app.config['SERVER_NAME'] = 'wupeiqi.com:5000'


                                                @app.route("/", subdomain="admin")
                                                def static_index():
                                                    """Flask supports static subdomains
                                                    This is available at static.your-domain.tld"""
                                                    return "static.your-domain.tld"


                                                @app.route("/dynamic", subdomain="<username>")
                                                def username_index(username):
                                                    """Dynamic subdomains are also supported
                                                    Try going to user1.your-domain.tld/dynamic"""

 

转载于:https://www.cnblogs.com/felixwang2/p/9262183.html

这段代码是一个装饰器工厂函数,用于给被装饰的函数添加路由规则。它的作用是将被装饰的函数作为视图函数添加到 Flask 应用程序中,并通过 `add_url_rule()` 方法设置相应的路由规则。 具体解释如下: ```python def route(self, rule: str, **options: t.Any) -> t.Callable: def decorator(f: t.Callable) -> t.Callable: endpoint = options.pop("endpoint", None) self.add_url_rule(rule, endpoint, f, **options) return f return decorator ``` - `def route(self, rule: str, **options: t.Any) -> t.Callable:`:定义了一个装饰器工厂函数 `route`,接受一个字符串参数 `rule` 和其他的关键字参数 `options`,并返回一个可调用对象。 - `def decorator(f: t.Callable) -> t.Callable:`:定义了一个装饰器函数 `decorator`,接受一个可调用对象 `f` 作为参数,并返回一个可调用对象。 - `endpoint = options.pop("endpoint", None)`:从 `options` 字典中获取键为 "endpoint" 的值,并将其赋给变量 `endpoint`。如果 "endpoint" 不存在,则将 `None` 赋给 `endpoint`。 - `self.add_url_rule(rule, endpoint, f, **options)`:通过 `add_url_rule()` 方法将被装饰的函数 `f` 添加到 Flask 应用程序中作为视图函数。`rule` 是路由规则,`endpoint` 是视图函数的名称(可选),`**options` 是其他的关键字参数,它们将被传递给 `add_url_rule()` 方法。 - `return f`:返回被装饰的函数 `f`。 通过使用这个装饰器工厂函数,可以以更简洁的方式给视图函数添加路由规则。例如,可以这样使用装饰器: ```python app = Flask(__name__) @app.route('/hello') def hello(): return "Hello, World!" if __name__ == '__main__': app.run() ``` 在上面的例子中,`@app.route('/hello')` 使用了 `route` 装饰器工厂函数,它会将被装饰的 `hello` 函数添加为 Flask 应用程序的视图函数,并设置了对应的路由规则。这种方式比直接调用 `@app.route()` 更灵活,可以通过传递不同的参数来定制路由规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值