类型检测
有时候,开发者希望对url参数中的某一段进行类型检测,框架提供了这种能力。
@app.route('/<tag:int>')
async def helloWorld(request, tag=None):
return response.html('''
<div>
<h1>
hello, {}
</h1>
</div>'''.format(tag))
在参数后面,提供一个类型:type
那么框架就会自动为你检测这个url的类型是否符合你的预期,并把正确的类型解析到你的函数的参数中。
上述例子中,当你填写的URL并不是预期的
int
类型,框架就会返回404
,页面未找到错误。
目前框架支持的URL类型有
:string 字符串类型
:int 类型
:number 类型