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

边栏推荐
- 批量修改数据库等视频文件名称
- [No 2022 Shanghai Security Officer A Certificate Exam Question Bank and Mock Exam
- HAVE FUN | "SOFA Planet" spacecraft plan, the latest progress of source code analysis activities
- pytorch基础之 pytorch 模型开发模板
- 4 Module 3: Literature Reading and Research Methods
- MFC 进程间通信(共享内存)
- 并发编程之线程基础
- Day38 LeetCode
- 每周推荐短视频:你常用的拍立淘,它的前身原来是这样的!
- shell 脚本编程---入门
猜你喜欢
随机推荐
Oracle常用语句归纳_持续更新
CentOS7静默安装Oracle11g_转载
Four functional interfaces
MySQL must know and must know (primary articles)
MFC 进程间通信(共享内存)
用白嫖的Adobe正版软件,减少应届毕业生的慢就业、不就业等现象
实战noVNC全过程操作(包含遇到的问题和解决)
Oracle中如何用一个表的数据更新另一个表中的数据_转载
How to use svg-icon (svg-sprite-loader plugin)
Win10远程连接(实现多用户同时连接)
Linux中安装redis
MySQL索引
HAVE FUN | "SOFA Planet" spacecraft plan, the latest progress of source code analysis activities
Redis-使用jedis连接linux中redis服务器失败的解决方案
pytorch基础之 pytorch 模型开发模板
[Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module
HAVE FUN | “SOFA 星球”飞船计划、源码解析活动最新进展
基于TF-IDF 文本相似性实战 详细教程
The use of async (asynchronous) and await
StarUML使用心得









