flask路由传参的本质
flask路由传参的本质
from flask import Flask, url_for, redirect
app = Flask(__name__)
# @app.route('/')
def index(nid):
print(nid, type(nid))
return 'ojbk'
# app.route的本质就是在执行add_url_rule,其中的rule是路由,endpoint是路由起别名,view_func是响应函数
# 注意:如果endpoint不传就默认是响应的函数名
# 路由参数:有名分组,app.add_url_rule('/index/<int:nid>')响应函数必须用nid来接收
app.add_url_rule('/index/<int:nid>', endpoint='index1', view_func=index, methods=['GET', 'POST'])
# 路由参数methods可以控制该方法能有哪些被允许的请求方式
@app.route('/login', methods=['POST'])
def login():
# 用endpoint取路由要用到url_for 在flask中导入,也就是反向解析
# redirect(url_for('index1'))
print(url_for('index1'))
if __name__ == '__main__':
app.run()
Only you can control your future
You're not alone. You still have family,peopel who care for you and want to save you.