当前位置:网站首页>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>')
中的lang
The 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}'
这时候1
The 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
边栏推荐
- log4j2漏洞复现以及解决方案
- 每周推荐短视频:你常用的拍立淘,它的前身原来是这样的!
- [Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library
- 2022 Quality Officer-Civil Construction Direction-General Basic (Quality Officer) Exam Mock 100 Questions and Online Mock Exam
- 【Cron】学习:cron 表达式
- Oracle常用语句归纳_持续更新
- HAVE FUN | "SOFA Planet" spacecraft plan, the latest progress of source code analysis activities
- 让你代码越来越高大上的技巧——代码规范,你得知道
- 【嵌入式开源库】MultiButton的使用,简单易用的事件驱动型按键驱动模块
- Win10远程连接(实现多用户同时连接)
猜你喜欢
The use of async (asynchronous) and await
CentOS7静默安装Oracle11g_转载
代码在线审查(添加网页批注)的实现
阿里云无法远程连接数据库MySQL错误码10060解决办法_转载
BGP Comprehensive Experiment
关于ie下href有中文出现RFC 7230 and RFC 3986问题的研究
Application layer protocol - DNS
实战noVNC全过程操作(包含遇到的问题和解决)
HAVE FUN | "SOFA Planet" spacecraft plan, the latest progress of source code analysis activities
Switch and Router Technology-33-Static NAT
随机推荐
curl 命令调用接口demo
Thymeleaf
MySQL must know and must know (primary articles)
Switch and Router Technology-34-Dynamic NAT
2022 Quality Officer-Civil Construction Direction-General Basic (Quality Officer) Exam Mock 100 Questions and Online Mock Exam
log4j2漏洞复现以及解决方案
阿里云无法远程连接数据库MySQL错误码10060解决办法_转载
Keras与tensorflow 使用基础
Pytorch最全安装教程(一步到位)
Switch and Router Technology-35-NAT to PAT
guava RateLimiter均匀限流
pytorch基础之 pytorch 模型开发模板
HAVE FUN | “SOFA 星球”飞船计划、源码解析活动最新进展
并发编程之线程基础
元宇宙社交应用,靠什么吸引用户「为爱发电」?
基础数据之double和float区别
四大函数式接口
Tips to make your code more and more taller and taller - code specification, you have to know
Tips to improve your productivity, you have to know - Navitcat shortcuts
【动态代理】CGLIB 动态代理的使用及原理