Sanic 类的url_for()方法的API接口。

url_for() 方法/函数
定义
url_for(view_name: str, **kwargs)
根据视图名称和提供的值构建URL。
为了构建URL,必须将所有请求参数作为关键字参数提供,并且每个参数必须通过指定参数类型的测试。 如果不满足这些条件,将抛出URLBuildError。
非请求参数的关键字参数将包含在输出URL的查询字符串中。
参数
- view_name : 视图名称的字符串。
 - **kwargs:用于创建请求参数和查询字符串参数的键值对。
 
返回值
创建好的URL字符串。
异常
URLBuildError
例子
from sanic import Sanic                                                                                                                                                                                     
from sanic import response
app = Sanic(__name__)
@app.route('/')
async def index(request):
    # generate a URL for the endpoint `post_handler`
    url = app.url_for('post_handler', post_id=5)
    # the URL is `/posts/5`, redirect to it
    return response.redirect(url)
@app.route('/posts/<post_id>')
async def post_handler(request, post_id):
    return response.text('Post - {}'.format(post_id))
••••
if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8000, debug=True)
 
我的公众号:猿人学 Python 上会分享更多心得体会,敬请关注。
***版权申明:若没有特殊说明,文章皆是猿人学 yuanrenxue.con 原创,没有猿人学授权,请勿以任何形式转载。***

说点什么吧...