1.可传入参数:

@app.route('/user/<username>') #常用的 不加参数的时候默认是字符串形式的
@app.route('/post/<int:post_id>') #常用的 #指定int,说明是整型的
@app.route('/post/<float:post_id>')
@app.route('/post/<path:path>')
@app.route('/login', methods=['GET', 'POST'])
路由系统
    





            
 
DEFAULT_CONVERTERS = {
'default': UnicodeConverter,
'string': UnicodeConverter,
'any': AnyConverter,
'path': PathConverter,
'int': IntegerConverter,
'float': FloatConverter,
'uuid': UUIDConverter,
}
路由系统
    





            
 

2.反向生成URL: url_for

endpoint("name") #别名,相当于django中的name

路由系统
    





            
 
from flask import Flask, url_for

@app.route('/index',endpoint="xxx") #endpoint是别名
def index():
v = url_for("xxx")
print(v)
return "index"

@app.route('/zzz/<int:nid>',endpoint="aaa") #endpoint是别名
def zzz(nid):
v = url_for("aaa",nid=nid)
print(v)
return "index2"

 

1.可传入参数:

@app.route('/user/<username>') #常用的 不加参数的时候默认是字符串形式的
@app.route('/post/<int:post_id>') #常用的 #指定int,说明是整型的
@app.route('/post/<float:post_id>')
@app.route('/post/<path:path>')
@app.route('/login', methods=['GET', 'POST'])
路由系统
    





            
 
DEFAULT_CONVERTERS = {
'default': UnicodeConverter,
'string': UnicodeConverter,
'any': AnyConverter,
'path': PathConverter,
'int': IntegerConverter,
'float': FloatConverter,
'uuid': UUIDConverter,
}
路由系统
    





            
 

2.反向生成URL: url_for

endpoint("name") #别名,相当于django中的name

路由系统
    





            
 
from flask import Flask, url_for

@app.route('/index',endpoint="xxx") #endpoint是别名
def index():
v = url_for("xxx")
print(v)
return "index"

@app.route('/zzz/<int:nid>',endpoint="aaa") #endpoint是别名
def zzz(nid):
v = url_for("aaa",nid=nid)
print(v)
return "index2"

 

相关文章:

  • 2021-09-06
  • 2022-02-10
  • 2021-11-30
  • 2021-10-01
猜你喜欢
  • 2021-09-13
  • 2021-08-07
  • 2021-11-13
相关资源
相似解决方案