当前位置:网站首页>Flask (III) -- variable rules
Flask (III) -- variable rules
2022-06-11 09:27:00 【zju_ cbw】
You can dynamically build by adding variable parts to rule parameters URL. This variable part is marked as
<variable-name>. It is passed as a keyword parameter to the function associated with the rule .
In the following example ,route() The decorator's rule parameter contains the rules attached to URL /hello Of <name> Variable part . therefore , If you type URL: http://localhost:5000/hello/nhooo, that nhooo Will be provided as a parameter to hello() function .
from flask import Flask
app = Flask(__name__)
@app.route('/hello/<name>')
def hello_name(name):
return 'Hello %s!' % name
if __name__ == '__main__':
app.run(debug = True)
Run the top Python Script , Then open the browser and type URL http://localhost:5000/hello/nhooo. See in the browser Hello nhooo!
In addition to the default string variable section , You can also use the following converter to construct rules
| converter | describe |
|---|---|
| int | Accept integers |
| float | For floating point values |
| path | Accept slash as directory separator (/) |
In the following code , All these constructors are used .
from flask import Flask
app = Flask(__name__)
@app.route('/blog/<int:postID>')
def show_blog(postID):
return 'Blog Number %d' % postID
@app.route('/rev/<float:revNo>')
def revision(revNo):
return 'Revision Number %f' % revNo
if __name__ == '__main__':
app.run()
Run the above code , Access in a browser URL http:// localhost:5000/blog/11. You can see it in the browser Blog Number 11
Enter... In the browser URL http://localhost:5000/rev/1.1. You can see it in the browser Revision Number 1.100000
Flask Of URL Rule based Werkzeug The routing module of . This ensures the formation of URL Is the only one. , And based on Apache Established precedent .
Consider the rules defined in the following script
from flask import Flask
app = Flask(__name__)
@app.route('/flask')
def hello_flask():
return 'Hello Flask'
@app.route('/python/')
def hello_python():
return 'Hello Python'
if __name__ == '__main__':
app.run()
Both rules look very similar , But in the second rule , Use a trailing slash (/). therefore , It became a standard URL. therefore , Use /python or /python/ Return the same output . however , In the case of the first rule , URL: /flask/ It can lead to 404 Not Found page .
边栏推荐
- When the enterprise makes a decision, which part should lead the ERP project?
- 从企业评价的方历来看ERP软件成功与失利
- 【方案开发】红外体温计测温仪方案
- ERP体系的这些优势,你知道吗?
- Pulsar job Plaza | Tencent, Huawei cloud, shrimp skin, Zhong'an insurance, streamnational and other hot jobs
- Video review pulsar summit Asia 2021, cases, operation and maintenance, ecological dry goods
- 移动端页面使用rem来做适配
- Typescript high level feature 1 - merge type (&)
- Complexity analysis of matrix inversion operation (complexity analysis of inverse matrix)
- Day41 process pool and thread pool
猜你喜欢

Method (common method), method execution memory analysis, method overloading mechanism, method recursion

openstack详解(二十二)——Neutron插件配置

Augmented reality experiment IV of Shandong University

MSF adds back door to normal program

Day41 process pool and thread pool

Video review pulsar summit Asia 2021, cases, operation and maintenance, ecological dry goods

机器学习笔记 - 卷积神经网络备忘清单

Type-C扩展坞自适应供电专利维权案例

Install jupyter in the specified environment

【ROS】noedic-moveit安装与UR5模型导入
随机推荐
基于SIC32F911RET6设计的腕式血压计方案
1493. 删掉一个元素以后全为 1 的最长子数组
openstack详解(二十三)——Neutron其他配置、数据库初始化与服务启动
剑指 Offer II 041. 滑动窗口的平均值
How win10 Home Edition connects to remote desktop
机器学习笔记 - 卷积神经网络备忘清单
Customize PCBA scheme and develop wrist sphygmomanometer scheme
[FAQ for novices on the road] about data visualization
Sed explanation of shell script (SED command, sed -e, sed s/ new / old /...)
Telecommuting best practices and Strategies
Device = depthai Device(““, False) TypeError: _init_(): incompatible constructor arguments.
Openstack explanation (22) -- neutron plug-in configuration
【芯片方案】红外人体测温仪方案设计
Identifier keyword literal data type base conversion character encoding variable data type explanation operator
机器学习笔记 - 深度学习技巧备忘清单
682. baseball game
affair
Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)
企业决议时,哪个部分应该主导ERP项目?
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s