当前位置:网站首页>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

边栏推荐
猜你喜欢
随机推荐
Golden Warehouse Database KingbaseGIS User Manual (6.8. Geometry Object Input Function)
报表控件Stimulsoft报告中的数据矩阵条形码介绍
2022煤矿瓦斯检查考试题模拟考试题库及答案
Redis中RDB和AOF的区别
原生态mongo连接查询代码
(一)Docker安装Redis实战(一主二从三哨兵)
arraylist之与linkedlist
BGP Comprehensive Experiment
【Redis】Redis 的安装及图形化界面 Redis DeskTop Manager 的安装与使用
Difference between @Resource and @Autowired
【Mysql】----基础练习
log4j2漏洞复现以及解决方案
C Language: Practical Debugging Tips
阿里天池学习赛 新闻文本分类
面试宝典一: code题目记录
普林斯顿微积分读本05第四章--求解多项式的极限问题
@Resource和@Autowired的区别
You must understand - the nine built-in objects and four domain objects of JSP
Day38 LeetCode
【Cron】学习:cron 表达式









