当前位置:网站首页>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 .
边栏推荐
- MSF adds back door to normal program
- MySQL:Got a packet bigger than ‘max_ allowed_ packet‘ bytes
- Error [error] input tesnor exceeded available data range [neuralnetwork (3)] [error] input tensor '0' (0)
- Runtimeerror: blobreader error:the version of imported blob doesn't match graph_ transformer
- When the enterprise makes a decision, which part should lead the ERP project?
- MSF基于SMB的信息收集
- The mobile terminal page uses REM for adaptation
- Day44 database
- Talk about how to customize data desensitization
- Machine learning notes - spatial transformer network using tensorflow
猜你喜欢
![[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly](/img/c5/40ea5571f187e525b2310812ff2af8.png)
[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly

Type-C Bluetooth speaker single port rechargeable OTG solution

keyboard entry.

OpenCV CEO教你用OAK(四):创建复杂的管道

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

Sed explanation of shell script (SED command, sed -e, sed s/ new / old /...)

Control statement if switch for while while break continue

ArcGIS 10.9.1 geological and meteorological volume metadata processing and service publishing and calling

Ecological co construction | 2021 streamnational excellent partner of the year comes out!

报错device = depthai.Device(““, False) TypeError: _init_(): incompatible constructor arguments.
随机推荐
【ROS】noedic-moveit安装与UR5模型导入
企业需要考虑的远程办公相关问题
小型制氧机解决方案PCBA电路板开发
【分享】企业如何进行施行规划?
OpenCV OAK相机对比及介绍
Erreur de démarrage MySQL "BIND on TCP / IP Port: Address already in use"
Do you know these advantages of ERP system?
Machine learning notes - in depth Learning Skills Checklist
Automation operation and maintenance articles collection
682. baseball game
Type-C docking station adaptive power supply patent protection case
1854. 人口最多的年份
Augmented reality experiment IV of Shandong University
Video review pulsar summit Asia 2021, cases, operation and maintenance, ecological dry goods
考研数学 【数列极限证明题】题型方法总结
Identifier keyword literal data type base conversion character encoding variable data type explanation operator
报错Output image is bigger(1228800B) than maximum frame size specified in properties(1048576B)
[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly
Machine learning notes - the story of master kaggle Janio Martinez Bachmann
Opencv CEO teaches you to use oak (V): anti deception face recognition system based on oak-d and depthai