当前位置:网站首页>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 .
边栏推荐
- Machine learning notes - the story of master kaggle Janio Martinez Bachmann
- Some learning records I=
- 844. compare strings with backspace
- 从企业评价的方历来看ERP软件成功与失利
- ArcGIS 10.9.1 geological and meteorological volume metadata processing and service publishing and calling
- Openstack explanation (XXIII) -- other configurations, database initialization and service startup of neutron
- 市场上的服装ERP体系到底是哪些类型?
- Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire
- Tissu. JS définit dynamiquement la taille de la police
- Set up redis highly available cluster environment
猜你喜欢

OpenCV OAK-D-W广角相机测试

Some learning records I=

Redis source code analysis hash object (z\u hash)

Bowen dry goods | Apache inlong uses Apache pulsar to create data warehousing

Type-C Bluetooth speaker single port rechargeable OTG solution

Detailed explanation of the difference between construction method and method

MSF evasion模块的使用

Openstack explanation (22) -- neutron plug-in configuration
![[FAQ for novices on the road] about data visualization](/img/a1/d15e286c3c886a8d3a4ac3eb165748.png)
[FAQ for novices on the road] about data visualization
![报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s](/img/54/f42146ae649836fe7070ac90f2160e.png)
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s
随机推荐
[intelligent development] scheme design and hardware development of sphygmomanometer
考研數學 【數列極限證明題】題型方法總結
Comparison and introduction of OpenCV oak cameras
Error [error] input tesnor exceeded available data range [neuralnetwork (3)] [error] input tensor '0' (0)
Design of wrist sphygmomanometer based on sic32f911ret6
Modularnotfounderror: no module named 'find_ version’
MSF evasion模块的使用
Package details
报错Output image is bigger(1228800B) than maximum frame size specified in properties(1048576B)
Day39 content summary
市场上的服装ERP体系到底是哪些类型?
Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them
【方案设计】基于单片机开发的家用血氧仪方案
Type-C蓝牙音箱单口可充可OTG方案
Openstack explanation (24) -- registration of neutron service
报错RuntimeError: BlobReader error: The version of imported blob doesn‘t match graph_transformer
【方案开发】红外体温计测温仪方案
[TiO websocket] III. The TiO websocket server can send messages to users anywhere
Exclusive interview with PMC member Liu Yu: female leadership in Apache pulsar community
Tissu. JS définit dynamiquement la taille de la police