~玉米糊~
慢慢来,也会很快。 非宁静无以志学,学什么都一样,慢慢打基础,找规律、认真、坚持,其余的交给时间。
随笔 - 117,  文章 - 17,  评论 - 1,  阅读 - 81843

1. 静态路由和动态路由有什么区别

路由:Url Path

http://localhost/abc/test.html

abc/test.html

静态路由:Path与路由函数一一对应

动态路由:多个Path与同一个路由函数对应

http://localhost/abc/test.html

http://localhost/xyz/test.html

 

不管访问哪一个Url,都会执行同一个服务端的路由函数

动态路由通过<...>指定动态传递的参数

2. 如何使用Flask实现动态路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# pip install flask
 
from flask import Flask
app = Flask('__name__')
 
# 静态路由
# 装饰器
@app.route('/')
def index():
    return '<h1>root</h1'
 
@app.route('/greet')
def greet():
    return '<h1>hello everyone</h1>'
 
@app.route('/greet/Bill')
def greetBill(name):
    return f'<h1>hello Bill</h1>'
 
# 动态路由
@app.route('/greet/<name>')
def greetName(name):
    return f'<h1>hello {name}</h1>'
 
'''
如果静态路由与动态路由有冲突,优先使用静态路由
'''
 
@app.route('/greet/<a1>/<a2>/<a3>')
def args1(a1, a2, a3):
    return f'<h1>{a1}{a2}{a3}</h2>'
 
@app.route('/greet/<a1>-<a2>-<a3>')
def args1(a1, a2, a3):
    return f'<h1>{a1}*{a2}*{a3}</h2>'
 
if __name__ == '__main__':
    app.run()

  

posted on   yuminhu  阅读(172)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示