当前位置:网站首页>flaks framework learning: adding variables to the URL
flaks framework learning: adding variables to the URL
2022-08-11 05:33:00 【weixin_42576837】
urlmarked as variable
通过把 URL 的一部分标记为 <variable_name> 就可以在 URL 中添加变量.标记的 part will beKeyword arguments are passed to the corresponding view function.
通过使用 < converter:variable_name > ,可以选择性的加上一个转换器,为变量指定规则(is the type of the specified variable)
看这个例子:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'index page'
'''将urlpart of is marked as a variable,url:/languages/this part as a variable as keyword arguments lang = value 传递到对应的视图函数中,lang=value '''
@app.route('/languages/<lang>')
def get_language(lang):
return f'语言是:{
lang}'
if __name__ == '__main__':
app.run()
浏览器输入:

可以看到urlPart of it is passed to the view function as a variable.这里要注意一下,The variable names in this part must all be the same.
指定变量的类型
默认情况下,使用@app.route('/languages/<lang>')中的langThe received parameter types are bothstring类型的,even if you type iturl是http://127.0.0.1:5000/languages/1,这里面的1是整数,但是langOnce received it becomesstring

If you want it to be an integer,我们可以使用**< converter:variable_name >** , 选择性的加上一个转换器,为变量指定规则.
转换器类型: 
这里修改为< int: lang >:
@app.route('/languages/<int:lang>')
def get_language(lang):
print(type(lang))
return f'语言是:{
lang}'
这时候1The type becomes an integer
还有一些其他的类型,比如path类型,It is possible to include slashes in the received parameters/,类似路径:
@app.route('/num/<path:pathStr>')
def get_pathStr(pathStr):
return pathStr

边栏推荐
猜你喜欢
随机推荐
MyEclipse数据库工具使用教程:使用驱动程序
2022年质量员-土建方向-通用基础(质量员)考试模拟100题及在线模拟考试
Delphi7学习记录-demo实例
Unity WebGL RuntimeError: integer overflow(整数溢出问题)
Switch and Router Technology - 36-Port Mirroring
[Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module
【嵌入式开源库】MultiButton的使用,简单易用的事件驱动型按键驱动模块
提升你工作效率的技巧,你得知道——Navitcat 快捷键
redis分布式锁
[Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library
Day38 LeetCode
[FPGA tutorial case 49] Control case 1 - FPGA-based PID controller verilog implementation
HAVE FUN | “SOFA 星球”飞船计划、源码解析活动最新进展
MySQL事务的概念
0708作业---商品信息
博客目录管理 :机器学习 深度学习 nlp
普林斯顿微积分读本05第四章--求解多项式的极限问题
2022 building welder (building a special type of work) examination questions and simulation test
Sub-database sub-table ShardingSphere-JDBC notes arrangement
guava RateLimiter uniform current limit









